Abstract Classes in Java with Example

What is Abstract Class ?
In java A abstract class is defined as the "Class may contain abstract method or may not contain abstract method but for the abstract classes object creation not possible ".
A class which is declare with abstract keyword is known as the abstract classes.

Important Point to Remember 

  • For the abstract classes object creation not possible.
  • Abstract Class may contain abstract method or may not contain abstract method.
  • Abstract class must declare with abstract modifier.
  • if the class contain the abstract method then write the implementation in child classes.
  • Inside the abstract class it is possible to declare the main method.
  • Inside the abstract Class it is possible to declare the constructor.
  • it is possible to override the abstract method to normal method.
  • it is possible to override normal method to abstract method.
  • Inside the abstract class it is possible to declare the variable.
  • Inside the abstract class it it possible to declare the instance block and static block.
  • In Child class it is possible to override the abstract method and declare user defined normal method.
Purpose of abstract classes:
In java abstract class is mostly used to provide a base for sub classes to extend and implement the abstract methods and override or use the implemented methods in abstract classes.

Example of Abstract class

Case:1 Class contain at least one abstract method 

//an example of abstract class in java
 public abstract class School //Abstract Class
 {
String name;
int rollnumber;
void student()
{
}
void student()
{
}
abstract void student(); //Abstract Method

}

Case:2 abstract Class does not contain abstract method   

public abstract class School//Abstract Class
 {
 {
String name;
int rollnumber;
void student()
{
}
void student()
{
}
}

Example:1 Abstract Class Example with object Creation

//abstract parent class
abstract class Animal
{
 //abstract method
public abstract void sound();
}

//Dog class extends Animal class
public class Dog extends Animal
{

 public void sound()
{
System.out.println("huhuhu");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
  }

  }

Example -2  In java Inside the abstract Class declaration of main method is possible 

 public abstract class School //Abstract Class

 {
public static void main(String args[])
{
System.out.println("Abstract main method ");
}
}

Example-3 In java inside the abstract classes it is possible to declare the constructor.

//abstract parent class
abstract class Animal
{
 Animal ()
{
System.out.println("abstract class constructor");
}
}

//Dog class extends Animal class
public class Dog extends Animal
{

Dog()
{
super();
System.out.println("Normal class constructor");
}
public static void main(String args[])
{
 new Dog();

  }

  }
output:
abstract class constructor
Normal class constructor




Hope !! The above Tutorial " Abstract class in java With Example " Helpful For you ...
Team,
QA acharya


This article is contributed by  " Sandeep Yadav." 
If you want to share your knowledge with us and  would like to contribute, you can mail your article to   qa.article@gmail.com.


Tags: abstract class in java  , Purpose of abstract class in java , example of abstract class , when to use abstract class in java, abstract classes , java abstract classes , abstract class example 








abstract class in java





Post a Comment

0 Comments