ReadingsSuperAndSubClassJavaReading PDF
ReadingsSuperAndSubClassJavaReading PDF
ReadingsSuperAndSubClassJavaReading PDF
@Override
public void printItem() {
System.out.println(itemName + " " + itemQuantity
+ " (Expires: " +
expirationDate + ")");
return;
}
}
public class ClassDerivationEx {
public static void main(String[] args) {
GenericItem miscItem = new GenericItem();
ProduceItem perishItem = new ProduceItem();
miscItem.setName("Smith Cereal");
miscItem.setQuantity(9);
miscItem.printItem();
perishItem.setName("Apples");
perishItem.setQuantity(40);
perishItem.setExpiration("May 5, 2012");
perishItem.printItem();
return;
}
}
Smith Cereal 9
Apples 40
(Expires: May 5, 2012)
The term derived class (or subclass) refers to a class that is derived from another class
that is known as a base class (or superclass). Any class may serve as a base class; no
changes to the definition of that class are required. The derived class is said to inherit
the properties of its base class, a concept commonly called inheritance. An object
declared of a derived class type has access to all the public members of the derived
class as well as the public members of the base class. The following animation
illustrates the relationship between a derived class and a base class.
A derived class can itself serve as a base class for another class. In the earlier
example, class FruitItem extends ProduceItem {...} could be
added.
A class can serve as a base class for multiple derived classes. In the earlier
example, class FrozenFoodItem extends GenericItem {...} could be
added.
A class can only be derived from one base class directly. For example, inheriting
from two classes as in class House extends Dwelling, Property
{...} results in a compiler error.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Assign courseStudent's name with Smith, age with 20, and ID with 9999. Use the print
member method and a separate println statement to output courseStudents's data.
Sample output from the given program:
/////////////////////////////////////////////
return;
ageYears = numYears;
return;
return;
idNum = studentId;
return;
return idNum;
return;
courseStudent.setName("Smith");
courseStudent.setAge (20);
courseStudent.setID (9999);
courseStudent.printAll();
///////////////////////////////////
BaseClass.java:
public class BaseClass {
public void printMembers() { // Member accessible by anyone
// Print information ...
}
DerivedClass.java:
public class DerivedClass extends BaseClass {
public void someOperation() {
// Attempted accesses
printMembers(); // OK
baseName = "Mike"; // OK ("protected"
above made this possible)
baseCount = 1; // ERROR
}
InheritanceAccessEx.java
public class InheritanceAccessEx {
public static void main (String[] args) {
BaseClass baseObj = new BaseClass();
DerivedClass derivedObj = new DerivedClass();
// Attempted accesses
baseObj.printMembers(); // OK
baseObj.baseName = "Mike"; // OK (protected also
applies to other classes in the same package)
baseObj.baseCount = 1; // ERROR
derivedObj.printMembers(); // OK
derivedObj.baseName = "Mike"; // OK (protected also
applies to other classes in the same package)
derivedObj.baseCount = 1; // ERROR
return;
}
Specifier Description
private Accessible by self.
protected Accessible by self, derived classes, and other classes in the same package.
public Accessible by self, derived classes, and everyone else.
no specifier Accessible by self and other classes in the same package
public : A class can be used by every class in the program regardless of the
package in which either is defined.
no specifier : A class can be used only in other classes within the same package,
known as package private.