ITC 1233 - OBJECT ORIENTED
PROGRAMMING
OBJECT ORIENTED CONCEPT 4: ABSTRACTION
Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
HAVE YOU HEARD ABOUT
ABSTRACTION?
WHAT IS MEANT BY ABSTRACTION?
SO, WHAT WOULD BE THE MAIN
IMPORTANCE OF USING ABSTRACTION?
ABSTRACTION
• Abstraction is the concept of hiding unnecessary
details and showing only the necessary details.
• In Java, abstraction is mainly achieved through
abstract classes and interfaces.
EXAMPLE: ABSTRACTION
To use a car, a driver doesn’t necessarily need to know all the
details about the internal working of a car. A driver only needs
basic driving capabilities to work with the interface provided by
the car which has functions like: turning, accelerating, breaking
etc. Once you know how to use the this interface, you can
drive many types of cars, even though different manufacturers
may implement these functions differently.
ABSTRACTION VS. ENCAPSULATION
• Encapsulation refers to data hiding (information
hiding) while Abstraction refers to detail hiding
(implementation hiding).
• While encapsulation groups together data and
methods that act upon the data, restricting access
to some components of an object, abstraction
deals with exposing the interface to the user and
hiding the details of implementation, while only
representing essential details.
ABSTRACTION VS. ENCAPSULATION
ABSTRACT CLASSES
• An abstract class is a class that is declared with the abstract keyword.
• An abstract class can have both abstract and concrete methods.
Concrete classes can only have concrete methods. Any class that
contains one or more abstract methods must be declared as an abstract
class using the abstract keyword. An abstract method is a method that is
declared without an implementation.
• There can be no object of an abstract class. An abstract class can not
be directly instantiated with the new keyword. However, the abstract
super-class reference can be used to refer to a sub-class object.
ABSTRACT CLASSES
• An abstract class can have constructors and parameterized
constructors. The default constructor is always present in an
abstract class.
• An abstract class can have variables.
• Abstract classes are mostly used for super-classes which are
never intended to be instantiated: ‘abstract super- classes’.
• A method declared abstract in an abstract super-class –
• must always be implemented in the sub-classes, thus making
overriding compulsory.
OR
• make the sub-classes themselves abstract.
EXAMPLE 01: ABSTRACT CLASSES
Shape.java
EXAMPLE 01: ABSTRACT CLASSES
Circle.java
EXAMPLE 01: ABSTRACT CLASSES
Test.java
EXAMPLE 02: ABSTRACT CLASSES
Since no implementation of run(), it should be abstract as well.
EXAMPLE 03: ABSTRACT CLASSES
WHY DO WE USE ABSTRACT CLASSES
Abstract classes are used heavily in Design Patterns.
• Main purpose is to provide an appropriate super-class from
which other classes can inherit and thus share a common
design.
Be careful not to over use abstract classes
• Every abstract class increases the complexity of your design.
• Every subclass increases the complexity of your design.
• Ensure that you receive acceptable return in terms of
functionality given the added complexity.
ACTIVITY 01
• Choose and discuss a real world scenario where you can
apply abstraction using abstract classes.
• Explain 02 scenario specific advantages regarding the
scenario chosen.
INTERFACES
• Interfaces are declared using the interface keyword.
• Like a class, an interface can have methods and variables,
but the methods declared in an interface are by default
abstract only. A variable in an interface is by default final,
public and static only (constant class variable).
• Variables must be initialized.
• Interfaces are implemented by classes using the implements
keyword.
• There can be no object of an interface. An interface can
not be instantiated. An interface does not have a
constructor, not even a default constructor.
INTERFACES
Any class that implements an interface –
• must implement all its abstract methods in the class.
OR
• make the class itself abstract.
From Java 8 onwards, static methods are allowed in
interfaces.
EXAMPLE: INTERFACES
MyInterface.java
EXAMPLE: INTERFACES
Test.java
WHY DO WE USE INTERFACES?
• Interfaces are used to achieve total abstraction.
• Java normally does not support for multiple inheritance. By using
interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
ABSTRACT CLASSES VS. INTERFACES
ACTIVITY 02
Employee Management System:
• Design an abstract class Employee with attributes like name,
employeeID, and abstract methods like calculateSalary() and
displayInfo(). Leave the methods unimplemented.
• Create concrete classes Manager and Programmer that extend the
Employee class. Implement the calculateSalary() and displayInfo()
methods for each class. Include additional attributes specific to each
type of employee.
• Define an interface ProjectManagement with methods like assignTask()
and trackProgress(). Implement this interface in the Manager class to
showcase how interfaces can provide additional functionalities.
• Define an interface CodingSkills with methods like writeCode() and
debugCode(). Implement this interface in the Programmer class to
demonstrate the flexibility of interfaces in multiple inheritances.
ANY QUESTIONS?