Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• Overview of inheritance
• Working with superclasses and subclasses
• Polymorphism and overriding methods
• Interfaces
• The Object class
Mammal
Warm blooded
Breathes air
Whale
Flukes
Flippers
• Overview of inheritance
• Working with superclasses and subclasses
• Polymorphism and overriding methods
• Interfaces
• The Object class
Shirt Trousers
getId() getId()
getPrice() getPrice()
getSize() getSize()
getColor() getColor()
getFit() getFit()
getGender()
setId() setId()
setPrice() setPrice()
setSize() setSize()
setColor() setColor()
setFit() setFit()
setGender()
display() display()
getId() getId()
getId()
display() display()
display()
getPrice() getPrice()
getPrice()
getSize() getSize()
getSize()
getColor() getColor()
getColor()
getFit() getFit()
getGender()
getId()
display()
getPrice()
getSize()
getColor()
getFit() getFit()
getGender()
// Constructor
public Clothing(int itemID, String description, char colorCode,
double price) {
this.itemID = itemID;
this.description = description;
this.colorCode = colorCode;
this.price = price; }
Syntax:
[class_modifier] class class_identifier extends superclass_identifier
... include code here to check that correct codes used ...
this.colorCode = colorCode;
}
}
Clothing
getId()
Shirt
display()
getFit() getPrice()
getSize()
getColor()
Trousers
getFit()
Socks
getGender()
Employee
getName()
getEmpId()
getSkills() getEmployees()
getEditType() getToolsUsed()
• Overview of inheritance
• Working with superclasses and subclasses
• Polymorphism and overriding methods
• Interfaces
• The Object class
So far you have seen the class used as the reference type for
the created object:
• To use the Shirt class as the reference type for the Shirt
object:
Shirt myShirt = new Shirt();
• But you can also use the superclass as the reference:
Clothing clothingItem1 = new Shirt();
Clothing clothingItem2 = new Trousers();
Full set of
controls
available
Subset of
controls
available
Superclass
Object
Reference
Clothing Trousers
getId() getId()
Only these display() display()
methods may Methods inherited
getPrice() getPrice() from superclass
be called
getSize() getSize()
getColor() getColor()
Superclass
Reference Cast
operation
Casting changes Clothing
the reference type. getId()
display()
getPrice()
getSize()
getColor()
Class
Reference Object
Trousers Trousers
getId() getId()
display() display() Methods inherited
getPrice() getPrice() from superclass
getSize() getSize()
getColor() getColor()
getFit() getFit() Methods unique to
All methods are the Trousers class
getGender() getGender()
now accessible.
The parentheses
around cl ensure The syntax for casting is
that the cast the type to cast to in
applies to this parentheses placed before
reference. the reference to be cast.
cl.display();
char fitCode = ((Trousers) cl).getFitCode();
System.out.println("Fit: " + fitCode);
}
getId() Object
display()
getPrice() Trousers
getSize()
getColor() getId() Methods inherited
getPrice() from superclass
getSize()
Class
Reference
Methods inherited
display()
Trousers from superclass and
getColor() overridden
getId()
display() getFit() Methods unique to the
getPrice() getGender() Trousers class
getSize()
getColor()
getFit()
getGender()
• Overview of inheritance
• Working with superclasses and subclasses
• Polymorphism and overriding methods
• Interfaces
• The Object class
Clothing
getId()
display()
getPrice()
getSize()
getColor()
Outdoors
Shirt Trousers Custom Shirt
getId()
getFit() getFit()
display()
getGender()
getPrice()
getWeight()
Outdoors
Shirt Trousers Custom Shirt
getId()
doReturn() doReturn()
display()
getFit() getFit()
getPrice()
getGender()
getWeight()
Superclass
Object
Reference
Clothing Trousers
getId() getId()
Only these
methods can display() display()
Methods inherited
be called on getPrice() getPrice() from superclass
this reference getSize() getSize()
getColor() getColor()
Many classes
implementing
the List
interface
• Overview of inheritance
• Working with superclasses and subclasses
• Polymorphism and overriding methods
• Interfaces
• The Object class
StringBuilder
overrides Object's
toString().
First inherits
Object‘s
toString().
Second overrides
Object‘s
toString().