Tuesday 17 March 2015

Q 10) Abstract Class




Abstract Class:

  • Creation of a super class that only defines a generalized form (features of different sub classes) that will be shared by all of the sub classes, where in the subclass will fill up rest of the details.
  • An abstract class is a class that is declared with an “ abstract “ keyword.
  • An abstract class in never " Instantiated " meaning an instance of abstract class cannot be created.
  • It can have both abstract and non-abstract methods (methods without body and concrete methods).
  • Abstract Classes can have Constructors, member variables and normal (concrete) methods.
  • It is used to provide Abstraction.
  • Abstract Classes are not Interfaces.


Syntax of Abstract Class:

abstract class class – name {

     }
Abstract Method:

·       A method that is declared without any body within an abstract class is known as Abstract method.
·       The method can be defined by its sub class.
·       Abstract methods can never be final or static.
·       Any class that extends abstract class must implement all the abstract methods declared by the super class or make the child class abstract.

Abstract Method Syntax:

abstract return_type method_name();
//No definition.

Example Program of Abstract Class:

package org.javanoobs.abstractdemo;

public abstract class A {
    
     // This is the abstract method..
     abstract void callMe();
    
     //This is the normal or concrete method..
    
     public void normal(){
          
           System.out.println(" This is the normal or concrete method.. ");
     }
    
    

}

package org.javanoobs.abstractdemo;

public class B extends A {
     void callMe(){
          
           System.out.println(" This is the abstract call me method !!! ");
     }

    
public static void main(String[] args) {
    
    
     B b = new B();
     b.callMe();
     b.normal();
}
}

OUTPUT :
This is the abstract call me method !!!
This is the normal or concrete method..



No comments:

Post a Comment