Abstract Class in Java

0
Abstract Class in Java:-




An abstract class is the special class of JAVA which is used to perform Data Abstraction using class.

We can not create an object of abstract class hence abstract class never will be accessed directly .it will be accessed by derive class Object.

means if we want to hide the complete definition of class then we can create an abstract class.


Abstract class contains set of abstract method and normal method both.

An abstract method has only contained a method declaration and it will be defined by a derived class.

an abstract method will declare under abstract class or interface.


Syntax of the abstract method:-


access specifier abstract returntype methodname().

public abstract void display();


Syntax of an abstract class:-


abstract class Classname
{
      variable declaration;
       normal method;
       abstract method;
       static method;


}

abstract class does not contain a constructor.


Advantage of an abstract class:-

1 Data abstraction:-    we can not create object of abstract class hence abstract class hide the complete definition of class for direct accessibility.


2 Data Contract:-  all abstract methods (set of rules) must be defined under child class otherwise abstract class provide an error.


note:-  1)abstract class always will be parent class .it will never define as a child class.
           2)we should always create the reference of abstract class to access abstract data.


Abstract Class Example:-


abstract class Bank
{
    int balance=5000;
    void info()
    {
       System.out.println("Banking Operation");
    }
    abstract void login();
    abstract void credit();
    abstract void debit();
    abstract void checkbalance();
 
}

class Customer extends Bank
{
   void login()
   {
       System.out.println("login");
   }

   void credit()
   {
      System.out.println("credit");
    }

   void debit()
    {
      System.out.println("debit");
    }
   void checkbalance()
    {
     System.out.println("check balance");
    }

}

class Transaction
{
    public static void main(String args[])
    {
        //Customer obj = new Customer();  //not abstraction
        Bank obj = new Customer();  //abstraction
        obj.login();

    }


}



NOTE:- all static features and normal class features will be implemented by abstract class but we can not create an object of abstract class hence no need to define main() but if we want to define main() then we can define it under abstract class.

abstract class A
{
   public static void main(String args[])
   {
    // A obj = new A();
     System.out.println("A");
   }
}


























































Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)