Gadgets

Monday, 25 February 2013

Abstract Class and Method with Example


Abstract Methods:-
If methods has no body that methods called abstract methods.
Abstract methods create by using abstract keyword. We should be declare abstract methods in one class (super class) and implement in other class (sub class).
 abstract returntype abstractmethodname(par1,par2…..); 
Abstract Class:-
Ø  An abstract class is creating by using abstract keyword.
Ø  An abstract class contains instance variables & abstract methods and normal methods.
Ø  It is not possible to create objects to abstract class. But we can create a reference variable of abstract class.
Ø  All the abstract methods of the abstract class should be implemented in its sub classes.  If any method is not implemented, then that sub class should be declared as ‘abstract’.
Syntax:-
abstract class abstractclassname
{
// instance variables.
// normal methods and abstract methods.
}
Example Program :-
public class Abstract_Example {
    public void main(String arg[])
    {
       
    }
                
}
abstract class Animal{
    int legs,eyes;
    String name;
   
    abstract void Tiger();
    void Loin(){
        legs=4;
        eyes=2;
        name="lion";
        System.out.println("normal method details :legs "+legs+"eyes "+eyes+"name "+name);
    }
}
class Ani extends Animal{


    void Tiger() {
        legs=4;
        eyes=2;
        name="tiger";
        System.out.println("abstract method details :legs "+legs+"eyes "+eyes+"name "+name);
    }
   
}

No comments:

Post a Comment