0% found this document useful (0 votes)
4 views

Abstract classes Concepts in Java

The document provides an overview of abstraction in Java, explaining its definition, importance, and how it can be achieved through abstract classes and interfaces. It includes examples of abstract classes for vehicles and animals, illustrating how common properties and behaviors can be defined while allowing subclasses to implement specific details. Additionally, it outlines the rules for using abstract classes and answers common questions regarding their necessity and usage.

Uploaded by

shahmeasum007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Abstract classes Concepts in Java

The document provides an overview of abstraction in Java, explaining its definition, importance, and how it can be achieved through abstract classes and interfaces. It includes examples of abstract classes for vehicles and animals, illustrating how common properties and behaviors can be defined while allowing subclasses to implement specific details. Additionally, it outlines the rules for using abstract classes and answers common questions regarding their necessity and usage.

Uploaded by

shahmeasum007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Abstraction,

Abstract
Classes in JAVA
Outline
2

 Recap of previous lecture


 What is an abstraction
 Ways to achieve abstraction
 References
Abstraction (1/7)
• Abstraction is a process by which concepts are derived from the
usage and classification of precise ("real" or "concrete") concepts.
• Abstraction is a concept that acts as a super-categorical noun for all
subordinate concepts, and connects any related concepts as a group,
field, or category.
• Abstractions may be formed by reducing the information content of a
concept or an observable phenomenon, typically to retain only
information which is relevant for a particular purpose.
• For example, abstracting a leather soccer ball to the more general
idea of a ball retains only the information on general ball attributes
and behavior, eliminating the other characteristics of that particular
ball.
Abstraction (2/7)
• Example Vehicl
e
• Here we have different types of truck and car.
They have different color, shape, engine type
and purpose etc. that makes them distinct.
• But they have some common properties and
behavior among them i.e. they all have tires,
engine, steering, gear etc. They are used for
the travelling and can be operated in a
common way. Pickup
Box truck
• What should be the abstract concept for truck
these entities?
Sports Sedan
car car
• Vehicle can be the general idea of a car or a
truck. It retains only the information on general
vehicle attributes and behavior, eliminating the
other characteristics of a particular car or a
truck.
Abstraction (3/7)

Example Vehicle
• Here we have four classes Box truck, Classic
truck, Sports car and Sedan car.

• We can categorize them into two major


Truck Car
categories Truck and Car and can make
abstract classes which will contain all the
common properties and behaviors for trucks
and cars. Box truck Classic truck Sports car Sedan car

• We can further introduce abstraction for the


Truck and Car categories by adding a new
abstract class Vehicle that will hold common
properties and behaviors for its inheriting
classes.
Abstraction (4/7)

Example Anima
l

• Animal can be the


abstraction for a dog,
tiger, horse etc. It acts
as a super-categorical
noun.
• As an abstract concept it can
hold all the common properties
and behaviors present in
different species Do Tige Hors
g r e
Abstraction (5/7)

• Here we have the abstract class Animal. Animal


We have defined color and pattern color :
String
properties and sound, eat and run pattern :
String
behaviors which are present in all sound(

animals. ) eat()
run()

• The behaviors/ methods can be abstract


and the implementation can be
provided in the inheriting classes. Dog Tiger Horse
sound( sound() sound()
) attack()
• Now we have three concrete classes guard(
)
inheriting from the Animal class.
Overriding the sound behavior and
adding some behaviors specific to the
entities.
Abstraction (6/7)
Aircraf

Example t

• Aircraft can be the


abstraction for a
Propeller plane, Fighter
plane, Passenger plane
etc. It acts as a super-
categorical noun.

• 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()

land behaviors which are present in all


Aircrafts.

• 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

and behaviors specific to the entities. stop()


takeOff
stop()
takeOff
start()
stop()
() () takeOff()
land() land() land()
• For example in Propeller plane the fire()

number of propellers, in Fighter plane


the weapon and the fire behavior.
Ways to achieve abstraction

There are two ways to achieve abstraction in java.

1. Abstract class (0 to 100%)

2. Interface (100%) will discuss in next week


Abstract class

•A class that is declared using “abstract” keyword is known as abstract class.

•It can have abstract methods(methods without body) as well as concrete


methods (regular methods with body).

•A normal class(non-abstract class) cannot have abstract methods.

•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.

Why And When To Use Abstract Classes and Methods?


To achieve security - hide certain details and only show the important details of an
object.
Note: Abstraction can also be achieved with interfaces, which you will learn more
about in the next week.
Example
//abstract parent class
abstract class Animal
{
//abstract method
public abstract void sound(); •Now each animal must have a sound,
} by making this method abstract we
//Dog class extends Animal class
made it compulsory to the child class
public class Dog extends Animal
{ to give implementation details to this
public void sound() method. This way we ensures that
{
every animal has a sound.
System.out.println("Woof");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
}
}
Abstract class (Example with code)
abstract class Bike{ private int speed=70;
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear()
{System.out.println("gear changed");} //main method
} public static void main(String args[])
{
//Creating a Child class which inherits Abstract
Bike obj = new Honda();
class obj.run();
class Honda extends Bike{ obj.changeGear();
}
void run(){System.out.println("running safely..");
System.out.println(speed);}
}
Rules
• As we seen in the 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.

• 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 class is using an abstract method, they must be declared abstract. The


opposite cannot be true. This means that an abstract class does not
necessarily have an abstract method.

• If a regular class extends an abstract class, then that class must implement all
the abstract methods of the abstract parent.

• A class which contains 0 or more abstract methods is known as abstract class.


If it contains at least one abstract method, it must be declared abstract. Once
you declare a class abstract it indicates that the class is incomplete and, you
cannot instantiate it
Important Questions?

Is it necessary for abstract class to have abstract method?

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.

Why abstract class has no abstract method?

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?

• Abstract class provides a template for future specific classes.


• Use an abstract class to provide default behaviors (Code reusability).
• Abstract class defines common interface for its subclasses.
• Abstract operations (methods) can be declared and their implementation can be provided later
in the concrete classes.
• Abstract classes are useful when creating components because they allow us to specify an invariant
level of functionality in some methods, but leave the implementation of other methods until a specific
implementation of that class is needed.
• When creating a class library which will be widely distributed or reused especially to clients, use an
abstract class in preference to an interface; because it simplifies versioning. This is the practice used by
the Microsoft team which developed the Base Class Library.
References
21

• 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

You might also like