C
C
Core Java
Search…
23) Abstract Keyword

What is abstract Keyword?

abstract is a non-access modifier in java applicable for classes and methods but not for variables. It is used to achieve abstraction which is one of the pillars of Object Oriented Programming (OOP).
Following are different contexts where abstract can be used in Java.
    1.
    Abstract classes
    2.
    Abstract methods
Abstract classes
We know that every java program must start with a concept of class that is without classes concept there is no java program perfect.
In java programming we have two types of classes they are
1. Concrete class
2. Abstract class
Concrete class
A concrete class is one which is containing fully defined methods or implemented method.
Example
1
package com.gs.ilp.corejava.abstractKeyword;
2
3
public class Helloworld {
4
void display() {
5
System.out.println("Good Morning........");
6
}
7
}
Copied!
Abstract class
A class that is declared with abstract keyword is known as abstract class.
OR, the class which is having partial implementation (i.e. not all methods present in the class have method definition).
In short, an abstract class is one which is containing some defined method and some undefined method.
In java programming undefined methods are known as un-Implemented or abstract method.
Syntax
abstract class className
{
......
}
Example
abstract class Vehicle
{
......
}
Abstract method
An abstract method is one which contains only declaration or prototype but it never contains body or definition. In order to make any undefined method as abstract whose declaration is must be predefined by abstract keyword.
If any class has any abstract method then that class becomes an abstract class.
Example
class Vehicle
{
abstract void Bike();
}
Class Vehicle is become an abstract class because it have abstract Bike() method.
Key Points:
An instance of an abstract class cannot be created; we can have references of abstract class type though.
1
package com.gs.ilp.corejava.abstractKeyword;
2
3
abstract class Base {
4
abstract void fun();
5
}
6
7
class Derived extends Base {
8
void fun() {
9
System.out.println("Derived fun() called");
10
}
11
}
12
13
public class Main {
14
public static void main(String args[]) {
15
// Uncommenting the following line will cause compiler error as the
16
// line tries to create an instance of abstract class.
17
// Base b = new Base();
18
// We can have references of Base type.
19
Base b = new Derived();
20
b.fun();
21
}
22
}
Copied!
Output:
Derived fun() called
An abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of an inherited class is created.
1
package com.gs.ilp.corejava.abstractKeyword;
2
3
//An abstract class with constructor
4
abstract class BaseCLass {
5
BaseCLass() {
6
System.out.println("Base Constructor Called");
7
}
8
9
abstract void fun();
10
}
11
12
class DerivedClass extends BaseCLass {
13
DerivedClass() {
14
System.out.println("Derived Constructor Called");
15
}
16
17
void fun() {
18
System.out.println("Derived fun() called");
19
}
20
}
21
22
public class MainClass {
23
public static void main(String args[]) {
24
DerivedClass d = new DerivedClass();
25
}
26
}
Copied!
Output:
Base Constructor Called
Derived Constructor Called
We can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited.
// An abstract class without any abstract method
1
package com.gs.ilp.corejava.abstractKeyword;
2
3
abstract class BaseClass1 {
4
void fun() {
5
System.out.println("Base fun() called");
6
}
7
}
8
9
class DerivedClass1 extends BaseClass1 {
10
}
11
12
public class MainClass1 {
13
public static void main(String args[]) {
14
DerivedClass1 d = new DerivedClass1();
15
d.fun();
16
}
17
}
Copied!
Output:
Base fun() called
Abstract classes can also have final methods (methods that cannot be overridden).
// An abstract class with a final method
1
package com.gs.ilp.corejava.abstractKeyword;
2
3
abstract class BaseB {
4
final void fun() {
5
System.out.println("Derived fun() called");
6
}
7
}
8
9
class DerivedD extends BaseB {
10
}
11
12
public class MainM {
13
public static void main(String args[]) {
14
BaseB b = new DerivedD();
15
b.fun();
16
}
17
}
Copied!
Output:
Derived fun() called
Why can’t we create the object of an abstract class?
Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then what would happen? There would be no actual implementation of the method to invoke. Also because an object is concrete. An abstract class is like a template, so you have to extend it and build on it before you can use it.

Summary :

Last modified 2yr ago