In Java, an abstract class is one that has the abstract keyword stated in its declaration. Both abstract and non-abstract approaches are possible (method with the body). Through this article to understand the abstract class in Java. Let’s first comprehend Java’s abstraction before learning about the abstract class. Join SLA for the Best Java Training in Chennai.
Abstraction in Java
Abstraction is the technique of hiding implementation specifics from the user and just displaying functionality. A different method just displays the user’s needs and hides internal information, such as when sending an SMS, where you write the text and send the message. You are unaware of the internal operations involved in message transmission.
When an item is abstracted, you may concentrate on what it does rather than how it does it.
Abstraction Techniques
In Java, abstraction may be achieved in two different methods.
Abstract Class (0 to 100%)
Interface (100%)
Abstract Class in Java
An abstract class is one that has been declared to be such. Both abstract and non-abstract approaches are possible. It must be expanded, and its strategy put into practice. You can’t instantiate it.
Things to Keep in Mind
- An abstract class should always be declared with the abstract keyword.
- Both abstract and non-abstract approaches are possible.
You can’t instantiate it. - It is feasible to use constructors and static methods.
- It may have final methods that compel the subclass not to alter the method body.
Rules for using Java Abstract Class
- The Abstract class should be declared with an abstract keyword.
- The Abstract class will have abstract and non-abstract methods
- The Abstract class can’t be instantiated.
- The Abstract class will have the final methods
- The Abstract class will have constructors and static methods.
Example for Abstract Class
abstract class A{}
Abstract Method in Java
An abstract method is one that has been declared abstract and does not have an implementation.
Example of using the Abstract method abstract void printStatus();
Example for Abstract Class with Abstract Method
abstract class Car{
abstract void run();
}
class Tata extends Car{
void run(){System.out.println(“Running Smoothly”);}
public static void main(String args[]){
Car obj = new Tata();
obj.run();
}
}
Output
Running Smoothly
Real Scenario of Abstract Class
Most of the time, we are unaware of the implementation class (which is hidden from the consumer), and the factory function provides an object of the implementation class. A factory method is a method that gives back the class instance. The factory technique will be covered later.
In this example, the draw() function of the Rectangle class will be called if you create an instance of the class.
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();
}
}
Output
drawing circle
Finding Rate of Interest with Abstract Class in Java
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println(“Rate of Interest is: “+b.getRateOfInterest()+” %”);
b=new PNB();
System.out.println(“Rate of Interest is: “+b.getRateOfInterest()+” %”);
}}
Output
Rate of Interest is: 7%
Rate of Interest is: 8%
Abstract Class with Constructor, Data Member, and Methods
A data member, abstract method, method body (non-abstract method), function Object() { [Code] }, and even the main() method can all be found in an abstract class.
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
Interface with Abstract Class
The interface can also be implemented in some way using the abstract class. In this situation, the user may not be required to override every interface method.
interface A{
void a();
void b();
void c();
void d();
}
abstract class B implements A{
public void c(){System.out.println(“I am c”);}
}
class M extends B{
public void a(){System.out.println(“I am a”);}
public void b(){System.out.println(“I am b”);}
public void d(){System.out.println(“I am d”);}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Output
I am a
I am b
I am c
I am d
Conclusion
Hope this article helps you understand utilizing Abstract Class in Java. Learn practically in our Java Training Institute in Chennai as we follow the experimental-based learning approach for remote and classroom learners worldwide.