Java: Method Overiding

This entry is part 40 of 54 in the series Learn Java

Redefining an instance method in a class, which is inherited from the superclass, is called method overiding.

In the example below, Novel is a subclass of Book.  It has the same method name called printout. When we use instance nv to the access the printout method, it will access the method defined in the Novel class.

package javaapplication32;

class Book {

    public void printout() {
        System.out.println("This is the parent Book class");
    }
}

class Novel extends Book {

    public void printout() {
        System.out.println("This is the Novel class");
    }
}

public class MainBook {

    public static void main(String args[]) {
        
        Book bk = new Book();
        Novel nv = new Novel();
        
        bk.printout();
        nv.printout();      
    }
}

overide

 

Series Navigation<< Java: Calling base class constructor
Java: Accessing member in parent class >>