minte9
LearnRemember



Casting

In Java polymorphism is the ability of an object to take many forms.
 
/**
 * In Java the object can be different types (polymorphism).
 * 
 * To access a subtype method from a super type object ...
 * you must cast the object with subtype. 
 */

package com.minte9.oop.polymorphism;

public class Casting {
    public static void main(String[] args) {

        Animal a1 = new Dog(); // reference is an Animal
        Animal a2 = new Cat(); // object is a Cat

        System.out.println(a1.roaming()); 
        System.out.println(a2.eating());
        System.out.println((
            (Dog) a1).barking() // Look Here
        ); 

        // myDog is roaming (super method)
        // myCat is eating (super method)
        // myDog is barking (Dog method)
    }
}

class Animal {
    protected String name;
    public String eating() { 
        return name + " is eating (super method)"; 
    }
    public String roaming() { 
        return name + " is roaming (super method)"; 
    }
}

class Dog extends Animal {
    public Dog() { 
        name = "myDog"; 
    }
    public String barking() { 
        return name + " is barking (Dog method)"; 
    }
}

class Cat extends Animal {
    public Cat() { 
        name = "myCat"; 
    }
}

Parameter

You can declare the method parameter as a super-class type.
 
/**
 * Polymorphism and Dependency Injection
 * 
 * Using polymorphism we can pass object as parameters ...
 * and call object's super methods. 
 */

package com.minte9.oop.polymorphism;

public class Parameter {
    public static void main(String[] args) {

        File file = new File();
        Csv csv = new Csv();
        Xml xml = new Xml();

        file.open(csv); // CSV opened
        file.open(xml); // XML opened
    }
}

class Item {
    protected String type;
    public void open() {
        System.out.println(type + " opened"); // PO
    }
}

class Csv extends Item {
    public Csv() {
        type = "CSV";
    }
}

class Xml extends Item {
    public Xml() {
        type = "XML";
    }
}

class File {
    public void open(Item item) { // DI
        item.open();
    }
}



  Last update: 303 days ago