Abstract class in Java is a class that is declared using abstract keyword. It cannot be instantiated but can be extended into subclass. Abstract class cannot be instantiated means that new instances of an abstract class cannot be created. When an abstract class is extended into subclass, the abstract method of abstract class must be defined in the subclass.

In this section we are going to learn Abstract classes in Java with the help of many examples. Abstract classes are used for abstraction in the Java programming language. The abstract classes are used as a key mechanism for implementing the abstraction concept in the Java programming language. As you are already aware, abstraction is one of the object-oriented programming concepts that focuses on hiding the complex logic in the programming. This is used to show only the essential function to the developer and complex implementation is kept hidden.
Abstraction is also used to define the blueprint for other classes, which simply defines some methods and leaves other methods (abstract methods) to be implemented by subclasses.
Here is the video tutorial:
Facts about Abstract Classes in Java
- Abstract classes can't be instantiated
- Abstract classes can be extended into subclasses, where abstract methods are to be implemented. The subclass after implementation of all abstract methods defined in abstract classes can be instantiated and used.
- In abstract classes we may have methods with implementations along with abstract methods
- Abstract classes differ from interfaces as in interface we cannot add implementation while in abstract classes we may have method implementations
Here is the complete code Java code discussed in the video tutorial:
package net.roseindia.examples;
abstract class Demo{
abstract void hello();
public void sayHello() {
System.out.println("sayHello() from Demo class");
}
}
class DemoExample extends Demo{
void hello() {
System.out.println("Hello from DemoExample");
}
}
abstract class PrintMessage{
abstract void f1();
abstract void f2();
abstract void f3();
}
abstract class PrintDemo extends PrintMessage{
void f1() {
System.out.println("f1()");
}
}
class PrintDemoSecond extends PrintDemo{
@Override
void f2() {
System.out.println("f2()");
}
@Override
void f3() {
System.out.println("f3()");
}
}
public class AbstractClassExample {
public static void main(String[] args) {
System.out.println("Hello");
DemoExample d = new DemoExample();
d.hello();
d.sayHello();
PrintDemoSecond o = new PrintDemoSecond();
o.f1();
o.f2();
o.f3();
}
}
Abstract class in Java is a class that is declared using abstract keyword. It cannot be instantiated but can be extended into subclass.
Abstract class cannot be instantiated means that new instances of an abstract class cannot be created.
It is not necessary that an abstract class have abstract methods. An abstract method is declared without body but is followed by a semicolon.
If we have to define an abstract method under class then we have to make the class abstract as well.
All abstract methods must be public.
When an abstract class is extended into subclass, the subclass provides an implementation of the abstract methods in its parent class.
When an abstract class is extended into subclass, the abstract method of abstract class must be defined in the subclass.
Points of Abstract class :
- Abstract class may or may not contain abstract methods.
- An abstract class cannot be instantiated.
- Abstract classes can be extended into sub classes.
Abstract class can be declared as:
abstract class AbstractClass {
}
How to use of Abstract Method:
public class SecondClass extends AbstractClass
{
void display()
{
System.out.println("display() method implemented in SecondClass");
}
}
Abstract class Example:
AbstractClass.java
abstract class AbstractClass {
String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
abstract void display();
}
FirstClass.java
public class FirstClass extends AbstractClass
{
void display()
{
System.out.println("display() method implemented in FirstClass");
}
}
SecondClass.java
public class SecondClass extends AbstractClass
{
void display()
{
System.out.println("display() method implemented in SecondClass");
}
}
TestAbstractClass.java
class TestAbstractClass
{
public static void main(String args[])
{
FirstClass first = new FirstClass();
SecondClass second = new SecondClass();
System.out.println("\n*************Output*************");
System.out.println("Abstract Class Example");
first.setName("First Name : Rose");
second.setName("Second Name : India");
first.display();
System.out.println(first.getName());
second.display();
System.out.println(second.getName());
System.out.println("\n*************END****************");
}
}