Method Overloading
Def: Inheriting a method of super class and changing the implementation in sub-class is known as method overriding.
Question . Why method overriding?
Ans - To achieve run time polymorphism and code maintainability.
Question . How to achieve method overriding?
Ans - Re-write the same method signature of the super class and change the implementation in the sub class.
Example Of method overriding:
class BipinSir{//=============Example of Method Overriding=============
void property(){
System.out.println(" property of BipinSir");
}
void decision(){
System.out.println(" BipinSir Decides Pinki");
}
}
class Pintu extends BipinSir{
void decision(){ // Override decision method
System.out.println(" Pintu Decides Chumki");
}
}
class TestPintu{
public static void main(String[] args) {
Pintu p = new Pintu();
p.property();
p.decision();
}
}
Output:
property of BipinSir
Pintu Decides Chumki
Q. Which are the methods of superclass which can't be override by subclass?
1. Static method: static methods are class level
2. Final method: final keyword does not allow to change
Private method: private access within the class only.
No comments:
Post a Comment