If you are looking to understand Abstract classes in Java you are at right place.In this post we will see what is abstraction, abstract classes, abstract methods and how to create them in java.
What is Abstraction?
The process of hiding internal implementation details and only allowing to show the relevant data points is called Abstraction. For Example when you switch on the key of the car , car gets started but you don't know what is happening internally , the engine ignition etc. Similarly it is for Abstraction.Abstraction can be achieved using 2 ways in Java one is by using Abstract classes and other is using Interface.
What is Abstract class?
An Abstract class in java is a class who can have 1 or more abstract methods in it. Abstract classes cannot be instantiated meaning we cannot create object from the Abstract class. In order to create object the Class should be extended. An Abstract class can have non abstract methods as well.How to create Abstract class?
Abstract class can be created using abstract keyword in class declaration. A class should have at least 1 abstract method in-order to be called Abstract class.
abstract class KodeSrc{
abstract void greetings();
}
What are Abstract Methods?
A method that is declared with abstract keyword and doesn't have a body is called abstract method.Abstract methods should be declared inside abstract classes, you cannot declare an abstract method in non abstract class. An Abstract method doesn't have any body during its declaration.
What is the use of Abstract Methods?
- The Abstract methods are bound to be overridden , the sub class that extends the super class should provide the implementation for that method.
- Unless off course the sub class itself is declared as abstract, in this case you are just delaying the inevitable 😊 .
- Abstract methods decides the protocol of the method even if it doesn't have actual implementation, it sets the tone for that method. It is good for Polymorphism.
Benefits and Properties of Abstract Class:
- It helps in hiding the implementation details of a class.
- It is useful for Polymorphism .
- It works as a template for sub classes so that each sub class can have its own implementation for that property.
- We cannot create objects from an abstract class , meaning instantiation is not possible. Thus in order to make full use of abstract classes, the class should be extended by a sub class and the abstract method should be implemented.
- If the child class doesn't implement the abstract method then the child class should be also declared as abstract and all same set of rules applies to it as well now.
- Abstraction helps is code reusability and loose coupling of the code.
- Abstract classes are meant to be extended and Abstract methods are meant to be overridden
Let us verify what we have learnt and prove it via code.
So,
Can we create object of abstract class?
Ans : No
abstract class KodeSrc{
abstract void addString(String s1, String s2);
}
public class Main{
public static void main(String args[]){
KodeSrc kodesrc=new KodeSrc();
}
}
Output:
/Main.java:8: error: KodeSrc is abstract; cannot be instantiated
KodeSrc kodesrc=new KodeSrc();
^
1 error
What happens when we extend the abstract class but didn't provide the
implementation for abstract method?
Can we have a non abstract sub class that doesn't provide implementation to the abstract method?
Ans : No
abstract class KodeSrc{
abstract String addString(String s1, String s2);
}
class A extends KodeSrc{
}
public class Main{
public static void main(String args[]){
A a=new A();
System.out.println(a.addString("welcome","kodesrc"));
}
}
Output:
/Main.java:6: error: A is not abstract and does not override abstract method
addString(String,String) in KodeSrc
class A extends KodeSrc{
^
1 error
Correct way of implementing the abstract classes
Now Let's create 2 sub classes of the Abstract class give them there own
implementation of the abstract method and see what happens
abstract class KodeSrc{
abstract String addString(String s1, String s2);
}
class A extends KodeSrc{
String addString(String s1,String s2){
return s1+" "+s2;
}
}
class B extends KodeSrc{
String addString(String s1,String s2){
return s1+"-"+s2;
}
}
public class Main{
public static void main(String args[]){
A a=new A();
System.out.println(a.addString("welcome","kodesrc"));
B b= new B();
System.out.println(b.addString("welcome","kodesrc"));
}
}
Output:
welcome kodesrc
welcome-kodesrc
Okay ! So we cannot instantiate the Abstract classes fine! but
Can we create reference variable of Abstract Class in Java?
Ans: Yes we can create a reference variable of abstract classes. One of
the main use case for Polymorphism.
abstract class KodeSrc{
abstract String addString(String s1, String s2);
}
class A extends KodeSrc{
String addString(String s1,String s2){
return s1+" "+s2;
}
}
public class Main{
public static void main(String args[]){
KodeSrc a=new A();
System.out.println(a.addString("welcome","kodesrc"));
}
}
Output:
welcome kodesrc
Conclusion
In this post we have understood what is abstraction, abstract classes in java,
abstract methods, how to implement abstract classes.
Interested in understanding few more topics in Java?
Check it Out !
Please Let me Know, If you have any doubts.
Please Let me Know, If you have any doubts.