Overriding in Inheritance

suggest change

Overriding in Inheritance is used when you use a already defined method from a super class in a sub class, but in a different way than how the method was originally designed in the super class. Overriding allows the user to reuse code by using existing material and modifying it to suit the user’s needs better.


The following example demonstrates how ClassB overrides the functionality of ClassA by changing what gets sent out through the printing method:

Example:

public static void main(String[] args) {
 ClassA a = new ClassA();
 ClassA b = new ClassB();
 a.printing();
 b.printing();
}

class ClassA {
 public void printing() {        
     System.out.println("A");
 }
}

class ClassB extends ClassA {
 public void printing() {
      System.out.println("B");
 }
}

Output:

A
B

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents