



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




public String toString() public boolean equals(Object obj) protected Object clone() throws CloneNotSupportedException protected void finalize() throws Throwable
class Demon { public void type () { System.out.println("Demon."); } } class Vampire extends Demon { public void type () { System.out.println("Vampire."); } } class VampireWithSoul extends Vampire { public void type () { System.out.println ("Vampire with soul."); } } class DemonTest { public static void main(String[] args) { Demon demon = new Demon (); Vampire dracula = new Vampire (); VampireWithSoul angel = new VampireWithSoul (); demon.type (); // Calls Demon's version of type demon = dracula; demon.type (); // Calls Vampire's version of type demon = angel; demon.type (); // Calls VampireWithSoul's version of type } } /**
Example: class Demon { String demonType = "General Demon"; Demon() { System.out.println("[Demon init]"); } void weakness() { System.out.println("Arrogance."); } } public class Vampire extends Demon { String demonType = "Vampire"; Vampire() { super (); // Invoking the super class' constructor System.out.println("[Vampire init]"); } void display() { System.out.println(this.demonType); // Prints the current demonType ("Vampire") System.out.println(super.demonType); // Prints the demonType of the super class ("General Demon") } void weakness() { System.out.println("Sunlight, Stakes, Fire."); super.weakness(); // invoking the super class' method }
public static void main (String args[]) { Vampire vamp = new Vampire(); vamp.display(); vamp.weakness(); } } /**
class Demon { Demon() { System.out.println("[Demon init]"); } } class Vampire extends Demon { Vampire() { System.out.println("[Vampire init]"); } } class VampireWithSoul extends Vampire { VampireWithSoul() { System.out.println("[VampireWithSoul init]"); } }
class Pizza implements Eatable { public void eat () { System.out.println ("Mmmmm... Cheese!"); } } class Nugget implements Eatable { public void eat () { System.out.println ("Mmmmm... Crunchy!"); } } class Stone implements Eatable { public void eat () { System.out.println ("Wait, WTF am I eating?"); } } public class Test2 { public static void main (String [] args) { Eatable eatable = new Pizza (); // Interface reference to Pizza. eatable.eat (); eatable = new Nugget (); // Now pointing to Nugget Class. eatable.eat (); eatable = new Stone (); // Now pointing to Stone Class. eatable.eat (); } } Output: Mmmmm... Cheese! Mmmmm... Crunchy! Wait, WTF am I eating?