Abstract Classes & Methods
Course: Object oriented Programming (CE-225)
Course Teacher: Ms.Aneeta Siddiqui Ms.Samina Noureen
1
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
Topics Covered
Abstract class in Java
Abstraction in java
Abstract Method
3
4 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).
Before learning the Java abstract class, let's understand the
abstraction in Java first.
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
Abstract class (0 to 100%)
Interface (100%)
7 Abstract class in Java
A class which is declared as abstract is known as an abstract class.
It can have abstract and non-abstract methods.
It needs to be extended and its method implemented.
It cannot be instantiated.
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 of abstract class
# Example
abstract class A{}
11 Abstract Method in Java
A method which is declared as abstract and does not have
implementation is known as an abstract method.
# Example
abstract void printStatus();//no method body and abstract
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 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();
}
}
15 Abstract class declaration
An abstract class outlines the methods but not necessarily implements
all the methods.
# Example
//Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();
//This is concrete method with body
void anotherMethod(){
//Does something
}
}
16 Abstract class declaration
An abstract class outlines the methods but not necessarily implements
all the methods.
# 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.
A class derived from the abstract class must implement all those
methods that are declared as abstract in the parent class.
21 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).
22 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.
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
What would
happen?
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.
Example to demonstrate
26
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
27 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
28 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");
}
29 Abstract class vs Concrete class
A class which is not abstract is referred as 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 Abstract Class Interview
Questions and Answers
What is Abstraction in Java?
Ans: Abstraction in Java is a technique by which we
can hide the data that is not required to users. It
hides all unwanted data so that users can work only
with the required data.
How to achieve or implement Abstraction in
Java?
Ans: There are two ways to implement abstraction in
java. They are as follows:
a) Abstract class (0 to 100%)
b) Interface (100%)
What is Abstract class in Java? How to define
35
it?
Ans: An abstract class in java is a class that is
declared with an abstract keyword.
Example of Abstract class:
abstract class Test { abstract void show(); }
What is the difference between abstract class
and concrete class?
Ans: There are mainly two differences between an
abstract class and concrete class. They are:
a) We cannot create an object of abstract class. Only
objects of its non-abstract (or concrete) sub classes
can be created.
b) It can have zero or more abstract methods that
are not allowed in a non-abstract class (concrete
class).
36 What is Abstract in Java?
Ans: Abstract is a non-access modifier in java that is
applicable for classes, interfaces, methods, and inner
classes.
Can abstract modifier applicable for variables?
Ans: No.
Can an abstract method be declared as static?
Ans: No.(If you declare a method in a class abstract
to use it, you must override this method in the
subclass. But, overriding is not possible with
static methods. Therefore, an abstract method
cannot be static.)
Can an abstract method be declared with
37
private modifier?
Ans: No, it cannot be private because the abstract
method must be implemented in the child class. If
we declare it as private, we cannot implement it
from outside the class.
Can an abstract class have constructor?
Ans: Yes.
Is abstract class allow to define private, final,
static, and concrete methods?
Ans: Yes.
38
Is it possible to achieve multiple inheritance
through abstract class?
Ans: No.
Can we make an abstract class without
abstract keyword?
Ans: No, a class must be declared with abstract
keyword to make an abstract class.
Can we define an abstract method inside non-
abstract class (concrete class)?
Ans: No, we cannot define an abstract method in
non-abstract class.
39
Why abstract class has constructor even
though you cannot create object?
Ans: We cannot create an object of abstract class but
we can create an object of subclass of abstract class.
When we create an object of subclass of an abstract
class, it calls the constructor of subclass.
This subclass constructor has a super keyword in the
first line that calls constructor of an abstract class.
Thus, the constructors of an abstract class are used
from constructor of its subclass.
If the abstract class doesn’t have constructor, a class
that extends that abstract class will not get
compiled.