Use of Abstract Keyword in Java like Abstract Class
Hi friends, today we will discuss another important keyword of Java called Abstract keyword. Note down this important points of the Abstract keyword
- Abstract class acts as a base class for other classes of same type.
- Abstract class can’t be instantiated.
- There can be any abstract class without any abstract method but vice versa is not possible.
- If we are extending a class from an abstract class then we have to provide definition of all the abstract method in the first concrete class (concrete class means not abstract class).
- Abstract class can have constructors.
- Abstract class can have main method also.
Let’s do some practical on Abstract keyword. It will clear our doubts. After all learning through practical implementations will make our concept stronger.
Program 1
class abs1
{
void disp();
}
Output
This program will give a compile time error because it is a simple class program where we need to give definition to function of the class. Here we didn’t define the function of the class. Hence we get compile time error.
Program 2
class abs2
{
abstract void disp();
}
Output
Compile time error.
Reason :- abs 2 should be declared abstract. As I said before, if we declare any method inside the class as abstract then we have to declare the same class as abstract otherwise it will give a compile time error.
Program 3
abstract class abs3
{
abstract void disp();
}
Output
It will compile successfully.
Reason: – Since we have declared both method and class as abstract so there won’t be a problem.
Program 4
abstract class abs4
{
static void show()
{
System.out.println(“Hello”);
}
public static void main(String[] args)
{
show();
}
}
Output
Hello
Reason: – It will print the Hello statement. Here we have used a static keyword with the function, so we didn’t create instance of class. Now check out the program 5, it will make you things more clear.
Program 5
abstract class abs5
{
void show()
{
System.out.println(“Hello”);
}
public static void main(String[] args)
{
new abs5.show();
}
}
Output
Compile time error.
Reason: – We tried to instantiate and its clearly written in the above statement of abstract that Abstract class can’t be instantiated. This program 5 is almost same to program 4. Here we just removed the static keyword. Since the method or function is not static now, so we will need to instantiate to call the method but we are not allowed to instantiate in abstract class. So we get compiler time error.
Program 6
abstract class abs7
{
abstract void disp();
}class check extends abs7
{
public static void main(String[] ar)
{
new check().disp();
}
}
Output
Compile time error.
Reason: – Here we have created two class check and abs7. abs7 is the base class and abstract class. We are inheriting the abstract class here but we are not defining the abstract method in the derived class. Since we didn’t define the abstract class in the in the derive class it caused a compiler error. If you read the above statement of abstract class its clear written that “If we are extending a class from an abstract class then we have to provide definition of all the abstract method in the first concrete class (concrete class means not abstract class).”
Note: – I will update this post more later.
Share this Post[?]




