Abstract Class
Abstract class
Example :
abstract class Animal
{
abstract void eat();
void show()
{
System.out.println("This is Animal show");
}
}
class Demo
{
public static void main(String as[])
{
Animal a=new Animal(); //not allowed.
a.show();
}
}
Explanation :
» When you are unable to implement the method (Without body)
declare the method as abstract.
» When you declare one (or) more methods as abstract you must
declare the class also abstract.
» When class is abstract it can't be instantiated.
» In an abstract class we can have both abstract method and concrete
methods.
» In abstract class we can have only concrete methods also.
» When class is abstract we should write a subclass for that (or)
subclass is responsible to implement the abstract class with
subclass object only.
» We can call the subclass methods as well as abstract class concrete
methods.
» When you extend any abstract class we should override all the
abstract methods in subclass, otherwise declare the subclass
as abstract.
No comments:
Post a Comment