Runtime polymorphism will be implimented by the help of inheritance-Method Overriding
method overriding
Declaring a method in the subclass which already exists there in the parent class is known as method overriding.To override a method in a class, another method is created in its subclass that has the same signature(name,return type and parameter list) as the original method.
Remember the following point
- If the method is static, that method cannot be overridden
- If the method is declared in 'final' keyword that method cannot be overridden
Importance of ‘super’ keyword
The super keyword is used to access immediate parent class(base class) properties
If methods, variables and constructor are given the same name in the base class and the derived class, the base class property can be accessed using the super keyword.The super keyword should be used in the derived class only.Cannot be used in the main method
Method overriding-sample program
class Parent { void display() { System.out.println("This is parent class method display"); System.out.println("Parent class accessing through super keyword"); System.out.println("____________________________________"); } } class Child extends Parent { void display() { System.out.println(); System.out.println("This is child method display"); System.out.println("____________________________________"); super.display(); } } class Override { public static void main(String a[]) { Child obj=new Child(); obj.display(); } }
👍👍👍
ReplyDelete👍👍
ReplyDelete