0% found this document useful (0 votes)
13 views

Abstract Class and Abstract Methods

Uploaded by

arunanirmal2004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Abstract Class and Abstract Methods

Uploaded by

arunanirmal2004
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Abstract class and

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

• It is not possible to create an object of abstract class but it is possible to create a


reference variable for abstract class

• Abstract classes can be inherited and Abstract class may have constructor

• Any subclass of an abstract class must either


Implement all of the abstract methods in the superclass,
or
To be declared abstract itself.
Abstract method
• A method declared with abstract keyword is called abstract method.
• An abstract method does not provide implementation. Therefore, an
abstract method does not have method body. It only has declaration
• Syntax:
abstract returntype methodname(parameter-list);
• Example:
abstract void calculate(int total);
Abstract class and Abstract method
- Syntax
abstract class classname
{
abstract returntype methodname(parameter-list); //abstract method

returntype methodname(parameter-list)//non-abstract method


{
//body of the method
}
}
Example for the statements
“It is not possible to create an object of abstract class but it is possible to create a reference variable for
abstract class”

“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

Can an abstract method be final?


• No abstract method cannot be final. Every abstract abstract must be
overriden but the final keyword doesnot allow the method to be over
riden
Practice Question
• Design an abstract class Employee with an abstract method
calculateSalary().
• Create subclasses FullTimeEmployee and PartTimeEmployee that extend
Employee.
• Implement the calculateSalary() method in each subclass to compute the
salary based on different criteria.
• The FullTimeEmployee class should have attributes for monthlySalary.
• The PartTimeEmployee class should have attributes for hourlyRate and
hoursWorked.
• Write a main method to create instances of both employee types and
print their salaries.
Practice Question
• Create an abstract class Appliance with an abstract method turnOn().
Then, create subclasses WashingMachine and Refrigerator that
extend Appliance.
• The WashingMachine class should have an attribute for loadCapacity.
• Implement the turnOn() method in each subclass to simulate turning
on the appliance.
• Write a main method to create instances of each appliance and call
their turnOn() methods.

You might also like