Abstract Class and Abstraction
Abstract Class and Abstraction
abstraction
Abstract class in Java and Abstraction in Java
• abstract class A{}
• Abstract Method in Java
• A method which is declared as abstract and does not have
implementation is known as an abstract method.
• Example of abstract method
• abstract void printStatus();//no method body and abstract
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run()
{
System.out.println("running safely");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
• Create abstract class Fruit
• Add abstract taste() method
• Create Mango class
• Add taste() method in mango class
• Create main class FruitClass
• Add main method
• Create object of derived class
• Fruit f=new Mango();
• Access taste() method
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
s.draw();
}
}
by making the method abstract, and thus the class too, you signal clearly to users of this class that this class
should not be used as it is. Instead it should be used as a base class for a subclass, and that the abstract
method should be implemented in the subclass.