0% found this document useful (0 votes)
23 views

Abstract Class and Abstraction

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Abstract Class and Abstraction

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Abstract class and

abstraction
Abstract class in Java and Abstraction 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 is a process of hiding the implementation details and


showing only functionality to the user.
Abstract class in Java
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change
the body of the method.
Example of abstract class

• 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.

You might also like