The Java Platform Java Runtime Environment Data Types Control Statemets Methods Arrays Classes and Objects Inheritance![]() What is inheritance?![]() Inheritance![]() Inheritance![]() Subclass is a Superclass![]() Hiding Member Varaibles![]() Overiding Methods![]() Controlling Method Override![]() Final Classes![]() Abstract Classes![]() Assigning Subclass to Superclass![]() Assigning Superclass to Subclass![]() Recommended Reading Constructor Interface Packages & Access Modifiers Java Collections Framework | Assigning Subclass to SuperclassYou can put an object into a more general type abstract class GrObj { int x, y; public void moveTo(int x, int y) { this.x = x; this.y = y; } abstract void draw(); } class Rect extends GrObj { void draw() { System.out.println(toString()); } } class Circle extends GrObj { void draw() { System.out.println(toString()); } } public class Main {
public static void main(String[] args) {
GObj[] gobj = { new Rect(), new Circle() };
for (int i = 0; i < gobj.length; i++)
gobj[i].draw(); // Polymorphism
}
}
|