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:


Inheritance:
* Syntax
* Overriding in Inheritance

Table Of Contents
0 Inheritance
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap