CS102 Chapter5 ABSTRACT CLASSES AND INTERFACES
CS102 Chapter5 ABSTRACT CLASSES AND INTERFACES
CHAPTER 5
ABSTRACT CLASSES AND INTERFACES
07/11/2021 1
07/11/2021 OBJECT ORIENTED PROGRAMMING 2
Abstract Classes
Any class which contains at least a single abstract method is an abstract class.
Example
abstract int calculate();
➢ A concrete class inheriting from one or multiple abstract classes (indirectly), should implement all
the existing abstract methods
➢ Abstract classes will be used throw commun methods having the same name.
➢ Class variables
➢ Instance variables
➢ Local variables
➢ Methods
➢ Method’s parameters
➢ Classes
Example
class Test {
// Declaring and initializing static final variable
static final int CAPACITY = 4;
A final class
➢ Cannot be modified,
Example
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
1) To achieve security - hide certain details and only show the important details of an object
(interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class can implement
multiple interfaces.
• A concrete class should provide an implementation for all the methods declared in the interfaces in
question as well as those declared in its super-classes (abstract).