Abstract classes Concepts in Java
Abstract classes Concepts in Java
Abstract
Classes in JAVA
Outline
2
Example Vehicle
• Here we have four classes Box truck, Classic
truck, Sports car and Sedan car.
Example Anima
l
animals. ) eat()
run()
Example t
• As an abstract concept it
Propeller Fighter Passenger
can hold all the common plane plane plane
properties and behaviors
present in different aircrafts.
Abstraction (7/7)
Example
Aircraft
color :
String
engine :
Object
• Here we have the abstract class Aircraft. start()
We have defined color and engine stop()
takeOff(
properties and start, stop takeOff and ) land()
• Now we have three concrete classes Propeller plane Fighter plane Passenge plane
inheriting from the Aircraft abstract class. propellers : int weapon : Object
r
We have added some more properties start() start()
passengers : int
•We will learn what is a abstract class, why we use it and what are the rules that
we must remember while working with it in Java.
•An abstract class can not be instantiated, which means you are not allowed to
create an object of it. Why?
Abstract class
•An abstract class must be declared with an //Declaration using abstract keyword
abstract keyword. abstract class A
{
//This is abstract method
•It can have abstract and non-abstract methods. abstract void myMethod();
•It can have constructors and static methods also. //This is concrete method with body
void anotherMethod()
{
•It can have final methods which will force the //Does something
subclass not to change the body of the method. }
}
Why we need an abstract class?
•Lets say we have a class Animal that has a method sound() and the subclasses(see
inheritance) of it like Dog, Lion, Horse, Cat etc. Since the animal sound differs from one
animal to another, there is no point to implement this method in parent class. This is
because every child class must override this method to give its own implementation
details, like Lion class will say “Roar” in this method and Dog class will say “Woof”.
•So when we know that all the animal child classes will and should override this
method, then there is no point to implement this method in parent class. Thus, making
this method abstract would be the good choice as by making this method abstract we
force all the sub classes to implement this method( otherwise you will get compilation
error), also we need not to give any implementation to this method in parent class.
Why we need an abstract class?
•Since the Animal class has an abstract method, you must need to declare this class
abstract.
• 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).
Rules
• 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.
• If a regular class extends an abstract class, then that class must implement all
the abstract methods of the abstract parent.
It's not necessary for an abstract class to have abstract method. We can mark a
class as abstract even if it doesn't declare any abstract methods. If abstract class
doesn't have any method implementation, its better to use interface because
java doesn't support multiple class inheritance.
Yes we can have an abstract class without Abstract Methods as both are
independent concepts. Declaring a class abstract means that it can not be
instantiated on its own and can only be sub classed. Declaring a method abstract
means that Method will be defined in the subclass.
When to use abstract class?
• https://www.javatpoint.com/abstract-class-in-java
• https://www.geeksforgeeks.org/abstract-classes-in-java/?ref=lbp
• https://beginnersbook.com/2013/05/java-abstract-class-method/
22
THANK YOU