Super, Abstract
Super, Abstract
• The super keyword in Java is a reference variable which is used to refer immediate parent
class object.
• Whenever you create the instance of subclass, an instance of parent class is created implicitly
which is referred by super reference variable.
class Animal{
String color="white";
}
class Dog extends Animal{ Output
String color="black"; Black
White
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//
prints color of Animal class
} Animal and Dog both classes have a common
} property color. If we print color property, it
class TestSuper1{ will print the color of current class by default.
public static void main(String args[]){ To access the parent property, we need to
Dog d=new Dog(); use super keyword.
d.printColor();
}
}
super can be used to invoke parent class method
class Animal{
void eat(){
System.out.println("eating..."); }
} Output
class Dog extends Animal{ eating...
void eat(){ barking...
System.out.println("eating bread..."); }
void bark(){
System.out.println("barking..."); }
void work(){
super.eat(); Animal and Dog both classes have eat()
bark(); method if we call eat() method from Dog
} class, it will call the eat() method of Dog class
} by default because priority is given to local.
class TestSuper2{
public static void main(String args[]){ To call the parent class method, we need to
Dog d=new Dog(); use super keyword.
d.work();
}
}
super is used to invoke parent class constructor
class Animal{
Animal(){System.out.println("animal is created");}
} Output
class Dog extends Animal{ animal is created
Dog(){ dog is created
super();
System.out.println("dog is created");
}
} Note: super() is added in each class
class TestSuper3{ constructor automatically by compiler if there
public static void main(String args[]){ is no super() or this()
Dog d=new Dog();
}}
super keyword : Example
class Person{
int id; void display(){
this.id=id; }
float salary; }
} 1 ankit 45000
Abstract class in Java
• A class which is declared with the abstract keyword is known as an abstract class in Java.
• It can have abstract and non-abstract methods (method with the body).
Abstraction in Java
• Abstraction is a process of hiding the implementation details and showing only functionality to
the user.
• Another way, it shows only essential things to the user and hides the internal details
• The final keyword in java is used to restrict the user. The java final keyword can be
1.variable
2.method
3.class
1) Java final variable
If you make any variable as final, you cannot change the value of final
ariable(It will be constant).
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
Output:Compile Time Error
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
2) Java final method
If you make any method as final, you cannot override it.
class Bike{
final void run(){System.out.println("running");}
}
class Bike{
final void run(){
System.out.println("running...");
}
}
Output:running...
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
Static Binding and Dynamic Binding
int data=30;
• Here data variable is a type of int.
2) References have a type
class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
3) Objects have a type
An object is an instance of particular java class,but it is also an instance of its superclass.
class Animal{}
class Dog{
private void eat(){
System.out.println("dog is eating...");
}
Output:running...
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
Dynamic binding
When type of the object is determined at run-time, it is known
as dynamic binding.
class Animal{
void eat(){System.out.println("animal is
eating...");}
}