What is Abstract class in java?-vishmy1.blogspot.com

Abstract class in Java

A class that contains at least one abstract method is called as an abstract class. And as characteristics of class, we can not create objects of an abstract class.

In java, an abstract class definition must be followed by a keyword abstract.

Many times it is the situation where we want the derived classes to follow some methods with the same signature as of base class.


A class which contains one or more abstract methods must be defined with keyword abstract. Such classes are called as abstract classes.

To create an abstract method, the abstract keyword is used to create an abstract method.

If we want to make a class that only represents base class and we don't want to create an object of this class this can be achieved by creating an abstract class.

The abstract class can have an abstract method and a non-abstract method.

The abstract class can have at least one abstract method. 

Syntax:-

abstract class class_name
{
    abstract return_type method_name();
   access_modifier return_type method_name();{}
}

Simple Example of Abstract class:-

abstract class A
{
   abstract void set();
   void show()
   {
        System.out.println("this is simple method in abstract class:");
   }
}
class B extends A
{
   void display()
   {
      public void display("this is base class method:");
   }
  void  set()
  {
      system.out.println("abstract method is overridden in base class:");
 }
}
class AbstractClassExample
{
  public static void main(String args[])
 {
    B b1 = new B();
    b1.set();
    b1.show();
   b1.display();
 }
}

Note:- An abstract method is compulsory to override in the derived class.

Abstract Methods in Java:-

A method that is declared as abstract and doesn't have an implementation (body of the statement) is known as an abstract method.

Indirectly they share the same generalized method's signature, which is compulsory to be overridden by the derived classes. Such methods are called as an abstract method.

The implementation of the abstract method is return in abstract class is done in derived class.

Example of Abstract class in Java:-

abstract class A
{
   public abstract void add_number(int a, int b);
   public void sub_number(int x, int y)
  {
      int c = x-y;
      System.out.println(c);
  }
}
  class B extends A
{
    public void add_number(int a, int b)
    {
        int c = a+b;
       System.out.println(c);
   }
}
class AbstractMethodExample
{

   public static void main(String args[])
   {
     B b1 = new B();
    b1.add_number(2, 3);
   b1.sub_number(3, 2);
 }
}

In the above example we can see that class A is an abstract class and therefore it is not instantiated with any object and its abstract method add_number() is overridden in the derived class B.
Program for implementing Interface with abstract class:-


interface transport
{
  public void deliver();

}
abstract class animal

   public abstract void abstrmethod();
}
class tiger extends animal
{
   public void abstrmethod()
  {
     System.out.println("This is overridden abstract method in clss tiger");
 }
 class camel extends animal implemets transport
{
    public void deliver()
    {
       System.out.println("This is implemented deliver method in class camel");
   }
  public void abstrmethod()
  {
      System.out.priintln("This is overridden abstrAbstract method in class camel");
}
}
class test
{
   public static void main(String args[])
  {
      tiger t = new tiger();
     camel c = new camel();
    tiger.abstrmethod();
    camel.deliver();
   camel.abstrmethod();
 }
}

The transport interface declares a deliver() method. The abstract class animal is the superclass of the tiger, camel classes. The transport interface is implemented by the camel class. 


Note:- If you find anything incorrect, please write a comment in the comment section. This will help me to improve myself so that I can provide you better information.







Post a Comment

2 Comments