Implementation of Java method overriding
KW.java
class Fruit
{
    void cutting()
    {
        System.out.print("Cutting Fruit ");
    }  
}

class Mango extends Fruit
{
    void cutting()
    {
        super.cutting(); //invokes the super class method
        System.out.println("Mango");
    }
}
class Apple extends Fruit
{
    void cutting()
    {
        super.cutting(); //invokes the super class method
        System.out.println("Apple");
    }
}

class KW extends Fruit
{
    public static void main(String args[])
    {
        Fruit m=new Mango(); //Mango object
        m.cutting();
        Fruit a=new Apple(); //Apple object
        a.cutting();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Cutting Fruit Mango Cutting Fruit Apple kodingwindow@kw:~$
What Next?
Java Abstraction
Advertisement