0% found this document useful (0 votes)
2 views46 pages

Java Interfaces and Abstract Classes Explained

The document provides an overview of Java interfaces and abstract classes, detailing their definitions, properties, syntax, and differences. It explains when to use classes versus interfaces, the concept of multiple inheritance through interfaces, and the importance of access control in object-oriented programming. Additionally, it outlines the features and limitations of both interfaces and abstract classes, along with examples and observations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views46 pages

Java Interfaces and Abstract Classes Explained

The document provides an overview of Java interfaces and abstract classes, detailing their definitions, properties, syntax, and differences. It explains when to use classes versus interfaces, the concept of multiple inheritance through interfaces, and the importance of access control in object-oriented programming. Additionally, it outlines the features and limitations of both interfaces and abstract classes, along with examples and observations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Object Oriented Programming/

24CSE201/ Unit-2
Prof. Ajay Singh
Assistant Professor
Department of Computer Science &
Engineering
Devbhoomi Uttarakhand University
Java Interface
“An Interface in Java programming
language is defined as an abstract type
used to specify the behaviour of a class. An
interface in Java is a blueprint of a
behaviour. A Java interface contains static
constants and abstract methods”
Properties of Interface:
• The interface in Java is a mechanism to achieve abstraction.
• By default, variables in an interface are public, static and final.
• It is used to achieve abstraction and multiple inheritance in Java.
• Interfaces primarily define methods that other classes must
implement.
• An interface in Java defines a set of behaviours that a class can
implement, usually representing a CAN-DO relationship, but not
always in every scenario.
Syntax
• Private methods can only be called
inside default or static methods in
the interface, not by implementing
classes

• Static methods are also accessible


via the interface itself not through
objects
Example-
creating an
Interface
Interface
• To declare an interface, use the interface keyword. It is
used to provide 100% abstraction.
• That means all the methods in an interface are declared
with an empty body and are public and all fields are
public, static and final by default.
• A class that implements an interface must implement all
the methods declared in the interface. To implement the
interface, use the implements keyword.
Relationship Between
Class and Interface

• A class can extend another class


and similarly, an interface can
extend another interface.

• However, only a class can


implement an interface and the
reverse (an interface implementing
a class) is not allowed.
When to Use Class and
Interface?
• Use a Class when:
 Use a class when you need to represent a real-world entity with
attributes (fields) and behaviors (methods).
 Use a class when you need to create objects that hold state and
perform actions
 Classes are used for defining templates for objects with specific
functionality and properties.
When to Use Class and
Interface?
• Use a Interface when:

 Use an interface when you need to define a contract for behavior


that multiple classes can implement.
 Interface is ideal for achieving abstraction and multiple inheritance.
Example- Interface
• This example demonstrates how an interface can be used
to define common behavior for different classes (Bicycle
and Bike) that implement the interface.
Multiple Inheritance in Java Using Interface

• Java does not support multiple inheritance with classes to


avoid ambiguity, but it supports multiple inheritance using
interfaces.
Example
• This example demonstrates how a class can implement
multiple interfaces (Add and Sub) to provide functionality
for both addition and subtraction operations-
Advantages of Interfaces
• Without bothering about the implementation part, we can
achieve the security of the implementation.
• In Java, multiple inheritances are not allowed, however,
you can use an interface to make use of it as you can
implement more than one interface.
Features Class Interface

In an interface, you can't create an


Class Instantiation In class, you can create an object.
object

vs Variables are public static final


Variables Class can have instance variables
Interfa (constants only).

ce Methods Class can have concrete methods


In an interface, methods are
abstract by default

Inheritance It supports single inheritance Supports multiple inheritance

Constructors Can have constructors. No constructors allowed.

Supports private, protected, public, In an interface, all members are


Access Modifiers default. public by default

Keyword Defined using class. Defined using interface.

Default Methods It does not support default methods It supports default methods(JDK 8+)

Static Methods Can have static methods. Supports static methods (JDK 8+)

Private Methods Can have private methods. Supports private methods (JDK 9+).

Can have main() (since JDK 8, as


Main Method Can have main() for execution.
static methods are allowed).
Interface inheritance- Hierarchy Diagram
• The image below demonstrates a class can extend another class and
implement multiple interfaces, while an interface can extend multiple
interfaces-
Hierarchy Diagram
• This hierarchy will be followed in the same way, we cannot reverse
the hierarchy while inheritance in Java
• We cannot implement a class from the interface because Interface-to-
Class inheritance is not allowed
• It goes against the fundamental principles of class-based inheritance
• This will also breach the relationship known as the Is-A relationship,
where a subclass is a more specialized version of its superclass.
Interface Inheritance
• An Interface can extend another interface
Interface-Interface Inheritance
Example Program:
An interface can also extend multiple
interfaces
Abstract Class in Java
• In Java, abstract class is declared with the abstract keyword
• It may have both abstract and non-abstract methods(methods with
bodies)
• An ‘abstract’ is a Java modifier applicable for classes and methods in
Java but not for Variables
• Java abstract class is a class that can not be instantiated by itself
• It needs to be subclassed by another class to use its properties
Important observations about abstract classes
• An instance of an abstract class can not be created
• Constructors are allowed
• We can have an abstract class without any abstract method
• There can be a final method in abstract class
• But any abstract method in abstract class can not be declared as final
• We can define static methods in an abstract class
Important observations about abstract classes
• We can use the abstract keyword for declaring top-level classes (Outer
class) as well as inner classes as abstract
• If a class contains at least one abstract method then compulsory should
declare a class as abstract
• If the Child class is unable to provide implementation to all abstract
methods of the Parent class then we should declare that Child class as
abstract
• so that the next level Child class should provide implementation to the
remaining abstract method
Examples:
1. Example of Abstract Class & Abstract method
2. Abstract Class having constructor, data
member, and methods
Observation 1
• In Java, just like in C++ an instance of an abstract class cannot be
created, we can have references to abstract class type though. It is as
shown below via the Java program
Observation 2
• Like C++, an abstract class can contain constructors
in Java. And a constructor of an abstract class is
called when an instance of an inherited class is
created. It is as shown in the program below as
follows:
Observation 3
• In Java, we can have an abstract class without any
abstract method. This allows us to create classes that
cannot be instantiated but can only be inherited. It is
as shown below as follows with help of a java
program
Observation 4
• Abstract classes can also have final methods (methods that cannot be
overridden)
Observation 5
• For any abstract java class we are not allowed to create an object i.e.,
for an abstract class instantiation is not possible.
Observation 6
• Similar to the interface we can define static methods in an abstract
class that can be called independently without an object
Observation 7
• We can use the abstract keyword for declaring top-level classes (Outer
class) as well as inner classes as abstract
Observation 8
• If a class contains at least one abstract method- it is compulsory that
we should declare the class as abstract otherwise we will get a
compile-time error
• If a class contains at least one abstract method then, implementation is
not complete for that class
• hence it is not recommended to create an object so in order to restrict
object creation for such partial classes we use abstract keyword
Observation 8
- Example
Observation 9
• If the Child class is unable to provide implementation to all abstract
methods of the Parent class then we should declare that Child class as
abstract
• so that the next level Child class should provide implementation to the
remaining abstract method
Observation 9
Abstract Class vs Interface
Abstract Class vs Interface
Feature Abstract Class Interface
Blueprint with both abstract and Contract with abstract methods
Definition
concrete methods (default/static allowed since Java 8)
Can have abstract, concrete, static, Abstract by default; can have
Method Types
and final methods default and static methods (Java 8+)
Can have all types: static, final,
Variables Only public static final constants
non-final, non-static
Access Modifiers Supports private, protected, public Members are implicitly public
A class can extend only one A class can implement multiple
Inheritance
abstract class interfaces
Constructors Can have constructors Cannot have constructors
When you need shared code and When you need a strict contract
Use Case
partial implementation without implementation
Keyword Used abstract interface
Abstract class with italicized Interface with stereotype
UML Representation
method names <<interface>>
When to Use What
Scenario Recommendation
Shared behavior across subclasses Use Abstract Class
Multiple unrelated capabilities Use Interface
Need for future flexibility Prefer Interface
Partial implementation required Use Abstract Class
Access control
“Access control is foundational to object-oriented
programming. It helps us define what parts of our
code are exposed and what remains hidden”
What Is Access Control?
• Mechanism to restrict visibility of classes, methods, and variables
• Promotes encapsulation, security, and modular design
• Implemented via access modifiers
Types of Access Modifiers
Modifier Scope of Access
public Accessible from anywhere
protected Accessible within same package and subclasses
(default) Accessible within same package only
private Accessible only within the class
Code Example – All Modifiers
This class demonstrates all four
access levels. Try accessing
these members from another
class to see the restrictions in
action.
Access Across Packages
Same Subclass
Modifier Same Class Other Class
Package (diff pkg)
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌
(default) ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌

You might also like