public class Inherit { abstract class Stone { protected float weight = 13; protected int value = 4; abstract public String toString(); } class Aggregate { protected int numKind; } class Granite extends Aggregate { Granite() { weight = 25; numKind = 7; } public String toString() { return "Granite: weight=" + weight + " value=" + value + " numKind=" + numKind; } } Inherit() { Granite g = new Granite(); System.out.println(g); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { abstract class Figure { void display() { System.out.println("Figure"); } } class Line extends Figure { void display() { System.out.println("Line"); } } void tryme(Figure f) { f.display(); } Inherit() { Figure f = new Line(); tryme(f); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { abstract class Figure { void display() { System.out.println("Figure"); } } abstract class Rectangle extends Figure { } class Box extends Rectangle { void display() { System.out.println("Rectangle"); } } Inherit() { Figure f = (Figure) new Box(); f.display(); Rectangle r = (Rectangle) f; r.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { Figure() { System.out.println("Figure"); } } class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } class Box extends Rectangle { void display() { System.out.println("Box"); } } Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); r.display(); Box b = new Box(); b.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { void display() { System.out.println("Figure"); } } class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } class Box extends Figure { void display() { System.out.println("Box"); } } Inherit() { Figure f = new Figure(); f.display(); f = new Rectangle(); f.display(); f = new Box(); f.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { void display() { System.out.println("Figure"); } } class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } class Box extends Figure { void display() { System.out.println("Box"); } } Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); Box b = new Box(); f.display(); f = r; f.display(); f = b; f.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { void display() { System.out.println("Figure"); } } class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } void display(String s) { System.out.println(s); } } class Box extends Figure { void display() { System.out.println("Box"); } void display(String s) { System.out.println("This is printed: " + s); } } Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); Box b = new Box(); f.display(); f = r; f.display("one"); f = b; f.display("two"); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { void display() { System.out.println("Figure"); } } class Rectangle extends Figure { final void display() { System.out.println("Rectangle"); } } class Box extends Rectangle { void display() { System.out.println("Box"); } } Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); Box b = new Box(); f.display(); r.display(); b.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { class Figure { void display() { System.out.println("Figure"); } } class Rectangle extends Figure { void display() { System.out.println("Rectangle"); } } class Box extends Rectangle { void display() { System.out.println("Box"); } } Inherit() { Figure f = new Figure(); Rectangle r = new Rectangle(); Box b = new Box(); f.display(); f = r; ((Figure) f).display(); f = (Figure) b; f.display(); } public static void main(String[] args) { new Inherit(); } }
public class Inherit { abstract class Speaker { abstract public void speak(); } class Cat extends Speaker { public void speak() { System.out.println("Woof!"); } } class Dog extends Speaker { public void speak() { System.out.println("Meow!"); } } Inherit() { Speaker d = new Dog(); Speaker c = new Cat(); d.speak(); c.speak(); } public static void main(String[] args) { new Inherit(); } }