Abstract Full

Abstract:

Abstract is used to hiding the internal implementation of Java programming.

How to use ??

 "abstract" is a keyword that used to make the class as abstract class.


Levels:

  • Abstract at Class Level
  • Abstract at Method Level
  • Inheritance for abstract classes

Abstract at Class Level

    If the class with the abstract keyword that is known as abstract class.
Example:

public abstract class Testing
   {
      -..........................
                     ..................
    }

Method Levels

      We can use the abstract as method also. But the important thing is if you want to make your method as abstract then your class should be abstract. If it's not then the compiler shows the error.

Example:

public abstract class Testing
  {
     abstract void run();
  }


Note: The abstract method should not be implemented

i.e     public abstract class Testing
            {
               abstract void run()
                   {
                         System.out.println("This causes compilation error");
                   }
             }
Note: This program will never executed. Abstract method should not be implemented but it should be overridden by the subclass.

The abstract will support only abstract methods only ??

           No. The abstract method support both abstract method and the non-abstract method also.

Example:

public abstract class Testing
  {
     abstract void run();          //do not implement.
     void display()
        {
              System.out.println("This will executes normally");
        }
  }

Abstract for Inheritance Classes:

        As I already mention that, abstract should be override by the subclass itself. Let's the example will explain.

Example:

public abstract class Testing
  {
     abstract void run();   //abstract method

     void display()
       {
           System.out.println("This is legal");  // non-abstract method
  }

class Testing_Inherited extends Testing
   {
       
      void run()
         {
            System.out.println("The abstract method overridden");
         }
    }
 

Note: If you don't want to override the method in subclass then the subclass should be mention as abstract.











No comments:

Post a Comment