Home  >  Forum  >  Core Java
Post New Query

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


joined on
October 10,2013
Asked on March 21,2015
What is abstract class in java? How may I use it?
joined on
March 15,2013
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








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us