Object
Oriented
CS122
Programming
Mehran Yousaf
Lecture #
14
Abstraction
Abstractio
n
• Abstraction in Java refers to hiding the implementation details of a code
and exposing only the necessary information to the user
• It provides the ability to simplify complex systems by ignoring irrelevant details
and reducing complexity
• It allows programmers to focus on what an object does rather than how it does
it
• Real-time example of Abstraction
• ATM
• All are performing operations like cash withdrawal, money transfer, and retrieving mini
statements but don’t have information about internal details
Advantages of Abstraction
• Abstraction helps to
• Simplify complex systems by focusing on essential details
• Increase modularity and reusability of code
• Improve code organization and structure
• Focus on essential features and hide non-essential details
• Enhance code readability and maintainability
Implement Abstraction in Java
• Abstraction in Java can be achieved by:
• Abstract classes and abstract
methods
• Interfaces
Abstract Class
• An abstract class is a class that cannot be instantiated on its own and is meant
to be inherited by other classes
• Can contain abstract methods (declared without implementation).
• Can contain concrete methods (with implementation).
• An abstract class’s purpose is to provide an appropriate superclass from which
other classes can inherit and thus share a common design
• Make a class abstract by declaring it with the keyword abstract
• An abstract class normally contains one or more abstract methods
Abstract Method
• An abstract method is a method that is declared but not defined in a class
• Acts as a placeholder for methods that must be implemented in subclasses
• An abstract method is an instance method with the keyword abstract in
its declaration
• Abstract methods do not provide implementations
• A class that contains any abstract methods must be explicitly declared
abstract
even if that class contains some concrete (nonabstract) methods
• Each concrete subclass of an abstract superclass must also provide concrete
implementations of each superclass’s abstract methods
• Constructors and static methods cannot be declared abstract
Syntax
public abstract class className {
public abstract returnType methodName();
}
public class subclassName extends className {
public abstract returnType methodName()
{
}
}
When to use Abstract Classes
• Abstract classes should mainly be used when
• Need different implementations of attributes or methods across
different subclasses that implement these attributes or methods
• Have a design in mind but don’t know how to implement it yet
Abstraction Rules using Abstract Classes
and Methods
• Declare the abstract class and method with the abstract keyword
• Declare an abstract method without any implementation of the method
• An abstract class can contain concrete methods, along with abstract methods
• Overriding the abstract method in the subclass is compulsory or else the
Java compiler throws an error
• Cannot instantiate an abstract class. An object of abstract class cannot be
created
• Any class that does not implement the abstract method from the superclass
must also be declared as an abstract class
Programming Guidelines
• An abstract class declares common attributes and behaviors (both abstract and
concrete) of the various classes in a class hierarchy. An abstract class typically
contains one or more abstract methods that subclasses must override if they
are to be concrete. The instance variables and concrete methods of an
abstract class are subject to the normal rules of inheritance
• Attempting to instantiate an object of an abstract class is a compilation error
• Failure to implement a superclass’s abstract methods in a subclass is a
compilation error unless the subclass is also declared abstract