Abstract Class and Abstract Methods
Abstract Class and Abstract Methods
abstract methods
Presented by
Ms.G.Sakthi Priya, AP/CSE
Abstract class
• A class declared with abstract keyword is called abstract class.
• Abstract class may have both abstract and non-abstract
methods (methods with bodies).
• Object can’t be created for an abstract class. An abstract class cannot be
directly
instantiated with the new operator. Such objects would be useless, because an
abstract class is not fully defined.
• An abstract class must be extended (inherited) and its abstract methods
have to be implemented (over-ridden).
Syntax:
abstract class classname
{
Example:
abstract class student{
}
• Any class that contains one or more abstract methods must also be declared abstract
• you cannot declare abstract constructors, or abstract static methods
• Abstract classes can be inherited and Abstract class may have constructor
“Abstract classes can be inherited and Abstract class may have constructor”
All subclasses of Figure must override area( ). To prove this to yourself, try creating a subclass that does not override
area( ). You will receive a compile-time error.
Example for the statement:
Any subclass of an abstract class must either implement all of the abstract methods in the
superclass, or to be declared abstract itself.
abstract class Demo {
abstract void m1();
abstract void m2();
abstract void m3(); class Demo1{
} public static void main(String args[]){
abstract class FirstChild extends Demo { SecondChild s = new SecondChild();
public void m1() s.m1();
{ s.m2();
System.out.println("Inside m1"); s.m3();
} }
} }
class SecondChild extends FirstChild {
public void m2() {
System.out.println("Inside m2");
}
public void m3() {
System.out.println("Inside m3");
}
}
Exp 5
• Write a Java Program to create an abstract class named Shape that
contains two integers and an empty method named printArea().
Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the
classes contains only the method printArea( ) that prints the area of
the given shape.
Class poll
Can an abstract class be final?
• No abstract class cannot be final. Every abstract class must be
inherited but the final keyword doesnot allow the class to be
inherited