Abstract Classes and Abstract Methods
Abstract Classes and Abstract Methods
Lecture # 11
1
Course Books
Text Books:
Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
4
Abstract class in Java
5
Abstraction in Java
Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
Another way, it shows only essential things to
the user and hides the internal details,
For example, sending SMS where you type the
text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object
does instead of how it does it.
6
Ways to achieve Abstraction
There are two ways to achieve abstraction in
java
7
Abstract class in Java
8
Abstract class in Java
Points to Remember
An abstract class must be declared with an
abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the
subclass not to change the body of the method.
9
Abstract class in Java
10
Abstract class in Java
# Example
11
Abstract Method in Java
# Example
12
Example of Abstract class that has an
abstract method
In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
13
Example of Abstract class that has an
abstract method
In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{ Output:
abstract void run();
}
class Honda4 extends Bike{ running safely
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
14
Example of Abstract class that has an
abstract method
In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{ Output:
Hence for such
abstract kind of scenarios we generally declare the
void run();
class} as abstract and later concrete classes extend these
class Honda4 extends Bike{ running safely
classes and override the methods
void run(){System.out.println("running accordingly and can have
safely");}
public static void main(String args[]){
their own methods
Bike obj .
as well
= new Honda4();
obj.run();
}
}
15
Abstract class declaration
16
Abstract class declaration
# Example # Example
//Declaration using abstract //Declaration using abstract
keyword keyword
abstract class Instrument abstract class
{ StringedInstrument extends
protected String name; Instrument {
abstract public void play(); Protected int numberOfStrings;
} }
17
Abstract class declaration
# Example
Public class ElectricGuitar extends StringedInstrument {
Public ElectricGuitar() {
Super();
this.name= “Guitar” ;
this.numberOfstrings = 6;
}
Public ElectricGuitar(int numberOfStrings) {
Super();
this.name = “Guitar”;
this.numberOfstrings = numberOfstrings;
}
@override
public void play() {
System.out.println(“An Electric “ + numberOfStrings + “-string” + name + “ is
rocking! ”);}}
18
Abstract class declaration
# Example
Public class ElectricBassGuitar extends StringedInstrument {
Public ElectricBassGuitar() {
Super();
this.name = “Bass Guitar”;
this.numberOfStrings = 4;
}
Public ElectricBassGuitar(int numberOfStrings) {
Super();
this.name = “Bass Guitar”;
this.numberOfStrings = numberOfStrings ;
}
@Override
public void play() {
System.out.println(“An Electric “ + numberOfStrings + “-string” + name + “ is
rocking! ”);
}}
19
Abstract class declaration
# Example
Import main.java.music.ElectricBassGuitar;
Import main.java.music.ElectricGuitar;
public class Execution {
Public static void main(String[] args) {
ElectricGuitar guitar = new ElectricGuitar();
ElectricBassGuitarbassGuitar = new ElectricBassGuitar();
Guitar.play();
bassGuitar.play();
guitar = new ElectricGuitar(7);
bassGuitar = new ElectricBassGuitar(5);
guitar.play();
bassGuitar.play();
}}
20
Rules
Note 1: As we seen in the above example,
there are cases when it is difficult or often
unnecessary to implement all the methods in
parent class. In these cases, we can declare
the parent class as abstract, which makes it a
special class which is not complete on its own.
23
Do you know?
Since abstract class allows concrete methods
as well, it does not provide 100% abstraction.
You can say that it provides partial abstraction.
Abstraction is a process where you show only
“relevant” data and “hide” unnecessary details
of an object from the user.
24
Why can’t we create the object of an
abstract class?
Because these classes are incomplete.
They have abstract methods that have no body
So if java allows you to create object of this
class then if someone calls the abstract method
using that object then
25
Why can’t we create the object of an
abstract class?
There would be no actual implementation of the
method to invoke.
Also because an object is concrete. An abstract
class is like a template, so you have to extend it
and build on it before you can use it.
26
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} public static void main(String args[])
abstract public void {
anotherMethod(); //error: You can't create object of
} it
public class Demo extends AbstractDemo obj = new
AbstractDemo{ AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}
27
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} Output: public static void main(String args[])
abstract public void {
anotherMethod(); //error: You can't create object of
} Unresolved compilation problem:
it Cannot instantiate the
publictype
classAbstractDemo
Demo extends AbstractDemo obj = new
AbstractDemo{ AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}
28
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} Output: public static void main(String args[])
abstract public void {
Note: The
anotherMethod();class that extends
Unresolved compilation problem:
the abstract
//error: class,
You can't create
Cannot instantiate the
object of
} it
have to implement
publictype
class Demo extends all the abstract
AbstractDemo methods
AbstractDemo of it,
obj = new
else you have to declare that
AbstractDemo{ class abstract as well.
AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}
29
Abstract class vs Concrete class
30
Abstract class vs Concrete class
Key Points:
An abstract class has no use until unless it is
extended by some other class.
If you declare an abstract method in a class then
you must declare the class abstract as well. you
can’t have abstract method in a concrete class. It’s
vice versa is not always true: If a class is not having
any abstract method then also it can be marked as
abstract.
It can have non-abstract method (concrete) as well.
31
Abstract method in Java
For now lets just see some basics and example
of abstract method.
Abstract method has no body.
Always end the declaration with a semicolon(;).
It must be overridden. An abstract class must be
extended and in a same way abstract method must
be overridden.
A class has to be declared abstract to have abstract
methods.
32
Example of Abstract class and
method
class Demo extends MyClass{
/* Must Override this method while
extending
abstract class MyClass{ * MyClas
public void disp(){ */
public void disp2()
{
System.out.println("Concrete
System.out.println("overriding
method of parent class"); abstract method");
} }
abstract public void disp2(); public static void main(String
} args[]){
Demo obj = new Demo();
obj.disp2();
}
}
33
Example of Abstract class and
method
class Demo extends MyClass{
/* Must Override this method while
extending
abstract class MyClass{ * MyClas
public void disp(){ */
Output:
public void disp2()
{
System.out.println("Concrete
overriding abstract method
System.out.println("overriding
method of parent class"); abstract method");
} }
abstract public void disp2(); public static void main(String
} args[]){
Demo obj = new Demo();
obj.disp2();
}
}
34