ABstract Class
ABstract Class
Abstract class
• A class that is declared with abstract keyword,
is known as abstract class in java. It can have
abstract and non-abstract methods (method
with body).
• An abstract class is a placeholder in a class
hierarchy that represents a generic concept.
Vehicle
class TestBank{
public static void main(String args[]){
Bank b=new SBI();//if object is PNB, method of PNB will be invoked
int interest=b.getRateOfInterest();
System.out.println("Rate of Interest is: "+interest+" %");
}
}
Abstract Class with Constructor
Abstract class having constructor
• An abstract class can have
– Data Member
– Constructor
– Non Abstract Methods
– Abstract Methods
Example
abstract class Bike{
Bike(){
System.out.println("bike is created");
}
abstract void run();
void changeGear(){
System.out.println("gear changed");
}
}
class Honda extends Bike{
void run(){System.out.println("running safely..");
}
}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Output :
bike is created
running safely..
gear changed
Rules
• If there is any abstract method in a class, that
class must be abstract.