Abstract
classes and
methods with
examples
Outline
Recap
Abstract classes and methods with Examples
Overriding of Abstract Methods
Accesses constructor of Abstract Classes
why we can not instantiate abstract class
Uses and purpose
References
Recap
Abstract classes, why we use
Abstract Classes and Methods
Data abstraction is the process of hiding certain details and showing only
essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
An abstract class can have both abstract and regular methods:
Abstract Classes and Methods
ABSTRACT METHOD in Java, is a method that has just the method definition but does not
contain implementation.
A method without a body is known as an Abstract Method. The body is provided by the
subclass (inherited from).
It must be declared in an abstract class.
The abstract method will never be final because the abstract class must implement all the
abstract methods.
Abstract methods do not have an implementation; it only has method signature
Abstract Classes and Methods Syntax
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
System.out.println("Zzz");
}
}
From this example, it is not possible to create an object of the Animal class:
Animal myObj = new Animal(); // will generate an error
Abstract Classes and Methods Syntax
To access the abstract class, it must be inherited from another class.
Remember from the Inheritance lecture that we use the extends keyword
to inherit from a class.
Overriding of Abstract Methods
In Java, it is mandatory to override abstract methods of the superclass in
the subclass.
It is because the subclass inherits abstract methods of the superclass.
Since our subclass includes abstract methods, we need to override them.
Note: If the subclass is also declared abstract, it's not mandatory to
override abstract methods.
Example
// Abstract class
class Dog extends Animal
abstract class Animal {
{ public void animalSound()
{
// Abstract method (does not have a body) // The body of animalSound() is provided here
public abstract void animalSound(); System.out.println("The Dog says: wee woof");
}
// Regular method
}
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal) class MyMainClass
class Pig extends Animal {
{ public static void main(String[] args)
public void animalSound() {
{ Pig myPig = new Pig(); // Create a Pig object
// The body of animalSound() is provided here myPig.animalSound();
System.out.println("The pig says: wee wee"); myPig.sleep();
}
}
}
}
Accesses Constructor of Abstract Classes
Similar to non-abstract classes, we access the constructor of an
abstract class from the subclass using the super keyword.
abstract class Animal {
For example, Animal()
{
….
}
}
class Dog extends Animal { Here, we have used the super() inside the
Dog() constructor of Dog to access the
{ constructor of the Animal.
super();
... Note that the super should always be the
} first statement of the subclass constructor.
}
Another example
abstract class Animal
{
abstract void makeSound();
} class Main
class Dog extends Animal {
{ public static void main(String[] args)
public void makeSound() {
{ Dog d1 = new Dog();
System.out.println("Bark bark."); d1.makeSound();
}
} Cat c1 = new Cat();
class Cat extends Animal c1.makeSound();
{ }
public void makeSound() }
{
System.out.println("Meows ");
}
}
why we can not instantiate abstract class
Because an abstract class is an incomplete class (incomplete in
the sense it contains abstract methods without body and output)
we cannot create an instance or object; the same way you say for
an interface. You CAN instantiate an abstract class. You only need
to provide a concrete subclass.
Uses and purpose
A class which is declared using abstract keyword known as abstract class. An abstract class
may or may not have abstract methods. We cannot create object of abstract class. It is used to
achieve abstraction but it does not provide 100% abstraction because it can have concrete
methods.
To achieve security - hide certain details and only show the important details of an object.
What is purpose of abstract class?
The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class
from which other classes can inherit. Abstract classes cannot be used to instantiate objects and
serves only as an interface. Attempting to instantiate an object of an abstract class causes a
compilation error.
THANK YOU