Introduction
In this tutorial you’ll learn the following points:
- Abstract class
- Abstract methods
- Achieve abstraction in java
Abstract Class
Classes whose objects cannot be created or we can say classes that cannot be instantiated are known as abstract classes.
Abstract classes are declared using the abstract keyword.
Let’s understand this with the help of code
abstract class TUF { // data members //methods }
If we try to create an object for this class
Code:
Java Code
abstract class TUF {
// data members
//methods
}
class Main {
public static void main(String[] args) {
TUF obj1 = new TUF();
}
}
Output:
You might think, if we can’t create an object of abstract classes, how will we access the members of this class? The simple solution to this is – inheritance.
Let’s see how we can use inheritance to access the members of this class
Code:
Java Code
class Main {
public static void main(String[] args) {
striver st=new striver();
st.display();
}
}
abstract class TUF{
void display(){
System.out.println("TUF is takeuforward");
}
}
class striver extends TUF{
//code
}
Output: TUF is takeuforward
Here we created an object of the striver class and tried accessing the members of the display class.
According to the property of inheritance, the sub-class can access all the members of its superclass. Hence, this worked.
Accessing static members in abstract class
To access static members of the abstract class, we can directly use the reference of the abstract class. Following implementation shows the proof.
Code:
Java Code
abstract class TUF {
static void display() {
System.out.println("TUF class");
}
}
class Main {
public static void main(String[] args) {
TUF.display();
}
}
Output:
Constructors in Abstract Class
Since we can’t create an object of an abstract class, it shouldn’t be possible to call the constructor. But we can bypass this by using inheritance. Let’s see how we can do this. To call the constructor we can use super() within the definition of a sub-class constructor.
Code:
Java Code
abstract class TUF {
TUF() {
System.out.println("TUF class");
}
}
class striver extends TUF {
striver() {
super();
System.out.println("striver class");
}
}
class Main {
public static void main(String[] args) {
striver st = new striver();
}
}
Output:
An abstract class can have both abstract and non-abstract methods.
For example,
abstract class TUF { abstract void show1(); void show2() { //code } }
Abstract Methods
Abstract methods which can only be initialized are known as abstract methods. For example
abstract class TUF { abstract void display(); }
Abstract methods should be present only in abstract class. If the class is not declared abstract, then the compiler produces errors. For example,
Code:
Java Code
class TUF {
abstract void display();
}
class Main {
public static void main(String[] args) {}
}
Output:
Implementing abstract methods
If a class is inherited from an abstract class, it must implement the abstract methods of the superclass otherwise it should be declared as abstract as well. For example
Code:
Java Code
class Main {
public static void main(String[] args) {
striver st = new striver();
st.display();
}
}
abstract class TUF {
abstract void display();
}
class striver extends TUF {
public void display() {
System.out.println("TUF class method");
}
}
Output: TUF class method
If we try commenting out the implementation part of the display() method in the striver class, then the compiler would produce an error as follows:
Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]