Method Override

class Base3 {
	protected void disp() {
		System.out.println("it is disp in base");
	}
	public void disp1(int i) {
		System.out.println("It is disp1 in base");
	}
}
class Derived3 extends Base3 {
	/*
	 * Rules of overriding it is possible only in inheritance method name must
	 * be same in derive class return type must be same in overriden method
	 * access specifiers can not be narrow final method can not be overriden
	 */
	@Override
	public void disp() {
		System.out.println("it is disp in base");
	}
	@Override
	public void disp1(int i) {
		System.out.println("it is disp1 in derived");
		/*
		 * call disp1 method from base class super keyword refer object of base
		 * class Java supports single inheritance so only one base class is
		 * allowed
		 */
		super.disp1(i);
	}
}
public class OverrideDemo {
	public static void main(String[] args) {
		new Derived3().disp1(10);
	}
}
//output -
//it is disp1 in derived
//It is disp1 in base

Download

Java Basics Examples

Default constructor example
Parameterized constructor example
Call other constructor
Command Line argument example
Display sum of Command Line arguments
Foreeach example with int data type [ Only in JDK 1.5 ]
Foreeach example with String data type [ Only in JDK 1.5 ]
Foreeach example with user defiend [ Only in JDK 1.5 ]
Simple Inheritance
Call default constructor in Inheritance
Call parameterized constructor in Inheritance
Java Beans example
Overload method in one class
Overload method in inheritance
Override example , super keyword
super keyword example
override toString method


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code