What is abstract class in java? How may I use it?




Asked on March 21, 2015
What is abstract class in java? How may I use it?


Replied on April 12, 2015
1- A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

2- Abstract classes can have constructors which will be implicitly called when a child class is instantiated (if it is non-parameterised). But this is not possible with interfaces.

abstract class Animal{
    public int noOfLegs;
    public boolean isAlive;
    Animal(){
        isAlive = true;
    }
    public abstract void walk();
}
class Cow extends Animal{
    Cow(){
        noOfLegs = 4;
    }
    public void walk(){
        if(isAlive){
            //Code for walking   
        }
    }
}



Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us