Object Oriented Programming
Object Oriented Programming
OOP
ENG/Yousef saeed
Dr. Ayman Soliman
ID/235410
INTRODUCTION TO
INTERFACES
• Definition: An interface defines a set of methods a class must
implement, promoting abstraction and consistency.
• Features:
• Contains only method declarations (abstract methods).
• Enables multiple inheritance and polymorphism.
• Enforces a protocol of communication between classes.
WHAT IS AN
INTERFACE
• Definition:
An interface in Java is a reference type used to define a set of abstract
methods that a class must implement.
It represents a contract that enforces consistent behavior among
implementing classes.
• Key Points:
• Interfaces can contain abstract methods, default methods, static methods,
and constants.
• A class can implement multiple interfaces, supporting multiple inheritance.
• Interfaces provide a way to achieve full abstraction.
USES OF
INTERFACES
• Achieving Abstraction:
Interfaces allow defining methods without implementation, ensuring classes
provide specific behavior.
• Multiple Inheritance:
A class can implement multiple interfaces, overcoming the limitations of single
inheritance in Java.
• Standardization:
Interfaces define a contract that enforces consistent method implementation
across classes.
• Polymorphism:
Interfaces enable treating different objects uniformly if they implement the
same interface.
• Functional Programming:
Java 8 introduced functional interfaces (interfaces with a single abstract
method) for lambda expressions.
DIFFERENCES BETWEEN
INTERFACES AND CLASSES
CHARACTERISTICS OF
INTERFACES
• Abstract Nature:
Interfaces cannot have method implementations (Java 7 and
earlier). From Java 8, they can include default and static
methods.
• No Instantiation:
Interfaces cannot be directly instantiated.
• Multiple Inheritance Support:
A class can implement multiple interfaces, allowing flexibility in
design.
• Method Access:
All interface methods are public and abstract by default (unless
using default or static in Java 8+).
• Variables:
All variables in an interface are implicitly public, static, and final.
• Functional Interfaces:
Java 8 introduced interfaces with a single abstract method,
known as functional interfaces, used with lambda expressions.
INTERFACE
DECLARATION
• Definition:
An interface is declared using the interface keyword in Java.
• Structure:
• Contains abstract methods (no body).
• May include default and static methods (Java 8+).
• All methods are implicitly public and abstract (except
default/static).
• Variables are implicitly public, static, and final.
INTERFACE
IMPLEMENTATION
• Definition:
A class implements an interface using the implements keyword
and provides concrete implementations for all its abstract
methods.
• Key Points:
• A class can implement multiple interfaces.
• If a class does not implement all interface methods, it must be
declared abstract.
• Implementing a functional interface allows the use of lambda
expressions (Java 8+).
EXAMPLES OF
INTERFACE
• Real-World Examples:
1. Payment System: Different payment methods (credit card,
PayPal) implement a Payment interface.
2. Gaming: Different characters or objects implement a
Movable interface.
3. Messaging: Different communication methods (SMS, Email)
implement a MessageSender interface.
• Purpose:
• Ensures consistency across multiple implementations.
• Promotes flexibility by allowing interchangeability of
implementations.
MULTIPLE
INTERFACES
• Definition:
A class can implement multiple interfaces in Java, providing a
way to inherit behaviors from multiple sources.
• Key Points:
• Achieves multiple inheritance, which is not possible with
classes.
• The class must provide implementations for all abstract
methods of the interfaces.
• Interfaces can share common method names; the
implementing class must resolve conflicts.
INTERFACE
INHERITANCE
• Definition:
Interfaces can inherit from other interfaces using the
extends keyword, allowing one interface to build upon
another.
• Key Points:
• A child interface inherits all abstract methods and
constants of the parent interface.
• A class implementing the child interface must provide
implementations for all inherited methods.
• Supports multiple inheritance among interfaces.
EXTENDING MULTIPLE
INHERITANCE
• Definition:
In Java, an interface can extend multiple interfaces
using the extends keyword.
• Key Points:
• Supports multiple inheritance at the interface level.
• The child interface inherits all methods and constants
from the parent interfaces.
• A class implementing the child interface must
implement all methods from all parent interfaces.
INHERITANCE AND HIDING
CONSTANTS
• Definition:
In Java, constants in interfaces are implicitly public,
static, and final. When a child interface defines a
constant with the same name as one in a parent
interface, it hides the parent’s constant.
• Key Points:
• Inheritance:
Child interfaces inherit constants from parent
interfaces.
• Hiding:
A child interface can redefine a constant with the same
name, which hides the parent constant.
• Access:
Parent constants remain accessible using the parent
interface name.
ABSTRACT CLASSES VS INTERFACES
BEST PRACTICES FOR
INTERFACES