Lecture - 9 Abstract Classes and Abstract Methods
Lecture - 9 Abstract Classes and Abstract Methods
Contact Info:
Room No: BS-04, CED
1 Email: ulaila@ssuet.edu.pk
Course Books
Text Book:
Herbert Schildt, Java: The Complete Reference, 12th
Edition ,McGraw-Hill Education.
Deitel, Paul, Java How to Program, 11th Edition, Pearson,
2017
Reference Books:
Horton, Ivor, Beginning Java, 7th Edition, Wrox, 2011
2
Course Instructors
20
30
5
6 Abstract class in Java
A class which is declared with the abstract keyword is known as an
abstract class in Java. It can have abstract and non-abstract methods
(method with the body).
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.
11 Abstract class in Java
12 Abstract class in Java
# Example
# Example
# 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();
}
}
15 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();
}
}
16 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 void
abstract such kind of scenarios we generally declare
run();
the class
} as abstract and later concrete classes
class Honda4 extends Bike{ running safely
extend
void run(){System.out.println("running safely");} the methods
these classes and override
public static void main(String args[]){
accordingly and
Bike obj = new can have their own methods as well
Honda4(); .
obj.run();
}
}
17 Abstract class declaration
# Example
//Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();
# 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;
} }
19 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! ”);}}
20 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! ”);
}}
21 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();
}}
22 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.
A class derived from the abstract class must implement all those
methods that are declared as abstract in the parent class.
23 Rules
Note 2: Abstract class cannot be instantiated which means you
cannot create the object of it.
To use this class, you need to create another class that extends this
class and provides the implementation of abstract methods.
Then you can use the object of that child class to call non-abstract
methods of parent class as well as implemented methods(those that
were abstract in parent but implemented in child class).
24 Rules
Note 3: If a child does not implement all the abstract methods of
abstract parent class, then the child class must need to be declared
abstract as well.
25 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.
26 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
What would
happen?
27 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.
Example to demonstrate
28
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
}
AbstractDemo obj = new
public class Demo extends
AbstractDemo();
AbstractDemo{
obj.anotherMethod();
}
public void anotherMethod() {
}
System.out.print("Abstract
method");
}
Example to demonstrate
29 that object creation of
abstract class is not
abstract classallowed
AbstractDemo{
public void myMethod(){
System.out.println("Hello");
}
Output:
public static void main(String args[])
abstract public void
{ Cannot instantiate the
Unresolved compilation problem:
anotherMethod();
type AbstractDemo //error: You can't create object of it
}
AbstractDemo obj = new
public class Demo extends
AbstractDemo();
AbstractDemo{
obj.anotherMethod();
}
public void anotherMethod() {
}
System.out.print("Abstract
method");
}
Example to demonstrate
30 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[])
Note: The class that extends
abstract public void
{ the abstract
anotherMethod();
class,
} have to
Unresolved implement
compilation problem: all the
//error:
Cannot You abstract
can't createthe
instantiate
AbstractDemo obj = new
object of it
publictype
classAbstractDemo
Demo extends
methods
AbstractDemo{
of it, else you have to declare that
AbstractDemo();
class abstract as well. } obj.anotherMethod();
public void anotherMethod() {
}
System.out.print("Abstract
method");
}
31 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.
33 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.
34 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();
}
}
35 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();
}
}