View Source

h3.Class Inheritance Code Samples


Learn about classes, member variables, methods, inheritance, nested classes, and more.

* Creating Classes
* Inheritance

h5.Creating Classes

* [AClass.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/AClass.java] declares an instance member variable, an instance method, a class variable, a class method, and main. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [Alpha.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/one/Alpha.java] declares members with various access levels (public, private, protected). For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [AlphaTwo.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/two/AlphaTwo.java] is a subclass of Alpha but in a different package. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [DeltaOne.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/one/DeltaOne.java] has methods and variables that can be used by other classes in the same package. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [DeltaTwo.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/two/DeltaTwo.java] is not related to and is in a different package from {{Alpha}} so can access only the public members of Alpha. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [Stack.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/Stack.java] implements a stack (data structure) whose items are added and removed in a last-in-first-out (LIFO) fashion. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

h5.Inheritance

* [Planet.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/Planet.java] contains one instance method and one class method. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [Subclass.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/Subclass.java] overrides a method and hides a variable in its super class. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].

* [Superclass.java|http://java.sun.com/docs/books/tutorial/java/javaOO/examples/Superclass.java] shows how to use super to invoke an overridden method. For supporting information see [The Java Tutorial: Classes and Inheritance|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html].


----

The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact