0% found this document useful (0 votes)
2 views

Object Oriented Programming

This document provides an overview of interfaces in Object-Oriented Programming, specifically in Java. It defines interfaces, outlines their features and uses, and discusses their differences from classes, including multiple inheritance and polymorphism. Additionally, it covers best practices for designing interfaces and summarizes key points about their role in achieving abstraction and consistency across implementations.

Uploaded by

a.o.a.w.ae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Object Oriented Programming

This document provides an overview of interfaces in Object-Oriented Programming, specifically in Java. It defines interfaces, outlines their features and uses, and discusses their differences from classes, including multiple inheritance and polymorphism. Additionally, it covers best practices for designing interfaces and summarizes key points about their role in achieving abstraction and consistency across implementations.

Uploaded by

a.o.a.w.ae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 17

SLIDE 1

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

• Design for Abstraction:


Use interfaces to define contracts and enforce consistent behavior across
multiple implementations.
• Keep Interfaces Simple:
Avoid adding too many methods; keep interfaces focused on a single
responsibility.
• Use Descriptive Names:
Interface names should clearly describe their purpose (e.g., Readable,
Movable).
• Prefer Interfaces Over Abstract Classes:
Use interfaces when multiple inheritance is needed or when behavior is
shared across unrelated classes.
• Default Methods Sparingly:
Use default methods only when extending an interface without breaking
existing implementations.
• Avoid Constants in Interfaces:
Use a separate class or enum for constants instead of defining them in
interfaces.
SUMMARY AND KEY
POINTS
• Definition:
An interface is a blueprint for a class, defining a set of methods that must
be implemented.
• Key Features:
• Supports full abstraction by default.
• Allows multiple inheritance using the implements keyword.
• Can include default and static methods (Java 8+).
• Common Use Cases:
• Enforcing a contract across different classes.
• Supporting multiple behaviors in unrelated classes.
• Achieving polymorphism and flexibility in design.
THANK
YOU!

You might also like