Java abstract class with Example
When we declare any class with abstract then this class known as abstract class. Abstract class can method can be used as abstract and non-abstract. In Interface all methods implicitly public abstract and we know abstract methods can’t be any body part.
Example:
abstract class First { abstract void printA(); void printAB() { System.out.println("This is non-abstract method of abstract class First"); } }
Here printA() is an abstract method where printAB() is a non-abstract method. So, abstract class provide both abstract and non-abstract method.
Now, another class (e.g. class Second) wants to use this abstract class then that class need to extends the First class. The class Second can’t extends more than one class because the class maintain single inheritance. After extends class “First” need to override method printA() because this is an abstract method. printAB() method can be or can’t be override because this is optional and this method is not abstract. For the interface class, when another class need to use interface class then all the method interface mandatorily need to override.
Example:
class Second extends First { @Override void printA() { System.out.println("This is override method abstract class First"); } }
In this part, printA() method is override as default same as class “First” printA() method. If class “First” is an interface then printA() method mendatorilly override as public.
Keep in mind abstract class has not any object references directly. If we want to create any abstract class object then we need to create inner class with override method of abstract class.
public class AbstractDemo { public static void main(String args[]){ Second second = new Second(); second.printA(); second.printAB(); } }
Here create object of second class to call the methods. As class”First” is abstract so we did not create any object of this class.
So abstract class main advantages are need not to mandatorily override all the methods in abstract class. Only which class are need in frequently we declare these methods as abstract latterly we use these methods as override and change methods body part as required.
Extra features of abstract class are given below using an program:
abstract class Initfirst { int dim1,dim2; Initfirst(int d1, int d2) { this.dim1 = d1; this.dim2 =d2; } abstract int sum(); abstract int mul(); } class Initsecond extends Initfirst { public Initsecond(int d1, int d2) { super(d1, d2); } @Override int sum() { return (dim1 + dim2); } @Override int mul() { return (dim1 * dim2); } } class Inittheird extends Initfirst { public Inittheird(int d1, int d2) { super(d1, d2); } @Override int mul() { return (dim1 * dim2); } @Override int sum() { return (dim1 + dim2); } } public class ClassDemo { public static void main(String args[]) { Initsecond initsecond = new Initsecond(2,1); Inittheird inittheird = new Inittheird(4,5); Initfirst initfirst; initfirst = initsecond; System.out.println("Sum is : "+initfirst.sum() + " " +"Mul is : "+initfirst.mul()); initfirst = inittheird; System.out.println("Sum is : "+initfirst.sum() + " " +"Mul is : "+initfirst.mul()); } }
Following the program abstract class Initfirst contructor can not called directly like
Initfirst initfirst = new Initfirst(1,2); But Initfirst class subclass can be called directly by creating object.
Initsecond and Inittheird both extends Initfirst so Initfirst construct called by super() which is used both subclasses in Initsecond and Inittheird.
In main method both subclasses object is created. As abstract class Initfirst can not be called directly so we can call it initialize as “ Initfirst “any_name”; Follwing program Initfirst initfirst;
So we can make call by writing Initfirst
As, Initfirst = initsecond;
Initfirst = inittheird;