Skip to main content

Method Overriding

Method overriding
  • Inheriting method from super class changing its implementation in sub class according to sub class specification is known as method overriding.
  • The sub class can change the implementation of super class method by overriding concept
  • While overriding method in sub class should retain same signature of super class and can change the implementation define in the method body.
  • Following method of super class can not override

1. static method

2. private method

3. final method


Method overriding Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
class Demo
{
	void test()
	{
		System.out.println("running test method");	
	}	
}
class Sample extends Demo
{
 
         void test()
	{	
		System.out.println("running overridden test method");
	}
 
}
 
public class MethodOveriding
{
 
	public static void main(String[] args)
	{
 
		System.out.println("**********************");
 
		Sample obj1=new Sample();
 
		obj1.test();// calling to non static test method
 
     	System.out.println("**********************");
 
	}
 
}
 
output:
 
**********************
running overridden test method
**********************