Abstract classes in Java
Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract classes are generic classes (or types of objects) used as a basis to create particular objects that conform to its protocol, or the set of operations it supports. Some important points regarding abstract classes
- An Abstract class is a class that contains abstract as well as concrete methods.
- An Abstract class can't be instantiated in other words. We cannot create the object of an abstract class.
- An abstract class is a class that has at least one abstract method, and maybe there can be some regular methods.
- An abstract class can have static methods and constructors.
- An abstract class can have final methods which will prevent the subclass from changing the body of the method.
- We can achieve 0 to 100 percent abstraction using Java abstract classes.
Declaration of abstract class
abstract class Demo { abstract method1(); void method2();{ }}
Rules for abstract class
Here are some points that should be remembered before you make an abstract class. We can call them as rules- To use an abstract class, we have to inherit it from the other class.
- To use an abstract class, you need to create another class which extends this class.
- A class derived from a parent abstract class must contain all the methods of the parent class.
- The object of the abstract class cannot be created.
- If the child class does not contain all the method of a parent class, then it should be declared as an abstract class.
Why we need an abstract class?
Let's assume a Student class that contains method info() and its subclasses, name, id etc.. Since every name contains a different id like 'a' contain id 111 and 'b' contains id 222, so there is no point to implement this method to parent class because every child class overrides the method to provide its implementation detail. So when we know that all the student classes will override this, then there is no point to implement this method in the parent class. Thus making this method abstract is a good option. By making this method abstract, we force all the child classes to implement this.Abstract methods
An abstract method is a method that has only the method definition. An abstract method does not have implementation and body. It defines only the signature of the method. Syntax abstract void method(double X, double Y); If a class contains at least one abstract method, then it should be declared as abstract. public abstract class Demo{ // declare fields // declare nonabstract methods abstract void method();} If an abstract class is subclassed, then subclass will provide the implementation for all abstract method of the parent class. However, if it will not provide the implementation then the subclass must be declared as abstract.Some scenarios of Abstract class
Example 1abstract class Food{ abstract void eat(); } //in abstraction, implementation is unknown by end user class Pizza extends Food{ void eat(){System.out.println("Pizza is fantastic");} } class Burger extends Food{ void eat(){System.out.println("Burger is delicious");} } //In abstraction, method is called by programmer or user class TestAbstraction{ public static void main(String args[]){ Food obj=new Burger();//In Abstraction, object is provided through method obj.eat(); } }Output
Burger is deliciousIn the given example, Food is the abstract class and its implementation is provided by the Pizza and Burger classes. Mostly, implementation detail is hidden for us (which is hidden to the end user), and an implementation class object is provided by the factory method. Example 2 This example explains that a Java program may contain an abstract class, an abstract method, method body (non-abstract method), data member, constructor, and even main() method. Have a look at the below example.
abstract class Pc{ Pc(){System.out.println("PC repaired successfully"); } abstract void performance(); void changeOS(){ System.out.println("Win 10 installation is successful"); } } //Creating a Child class which inherits Abstract class class Hp extends Pc{ void performance(){System.out.println("performance is Superb..");} } //Creating a Test class which calls abstract and non-abstract methods class TestAbstraction1{ public static void main(String args[]){ Pc obj = new Hp(); obj.performance(); obj.changeOS(); } }Output
PC repaired successfully Performance is Superb.. Win 10 installation is successful