OOP IN JAVA(MERHOD OVERRIDING AND " super" KEYWORD)
METHOD OVERRIDING
A subclass inherits all variables and methods from its super class. we can modify the superclass methods in subclass with the same method as defined in superclass. Some it is very important to modify superclass method in subclass to change or customize the implementation of superclass method.
Normally ; its means method overriding is a changing the body of the method coming from a superclass to a sub class.
note : especial thing is an overriding , only the method related to the object is always executed and also it can be used for runtime polymorphism
class Vehicle{void run(){
System.out.println("Vehicle is running");
}
}//Creating a sub (child) class
class Bike extends Vehicle{void run(){
System.out.println("Bike is riding");
}
}
//creating a object of child class
Bike SI = new Bike();
//calling the method with child class instanceSI. run();}
}
"super" keyword
The super keyword is used to call the constructor of the super class from a sub class
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
class Cloths{
String color="white";}class Shirt extends Cloths{String color="black";void printColor(){System.out.println(color);System.out.println(super.color);}}class TestSuper1{public static void main(String args[]){Shirt s=new Shirt();s.printColor();}}
Thank You!
Comments
Post a Comment