MOD9-10-OOP-FT
MOD9-10-OOP-FT
• Code Reuse
➢ is the practice of using existing code to develop new applications.
➢ It is a fundamental principle of software engineering that allows developers to save time and effort by
leveraging existing code libraries, frameworks, and modules.
1. Inheritance
2. Composition
3. Delegation
1) Inheritance
➢ is a mechanism that allows a class to inherit properties and methods from another class
➢ In this approach, a subclass (or derived class) is created by inheriting properties and methods from a superclass
(or base class). This enables the subclass to reuse the code from the superclass without having to rewrite it
Inheritance is useful when creating a family of related classes that share common functionality.
2) Composition
➢ is a mechanism that allows a class to contain instances of other classes as members.
➢ In this approach, a class is composed of one or more objects of other classes
➢ This enables the class to reuse the functionality of the contained objects without inheriting from them
➢ Composition is useful when creating complex classes that have a set of common behaviors, but the
relationships between the behaviors are not well defined.
3) Delegation
➢ is a mechanism that allows a class to delegate responsibility for a task to another class.
➢ In this approach, a class delegates the implementation of a method to another object that is responsible for
providing the implementation.
➢ This enables the class to reuse the functionality of the delegate object without having to implement it itself.
➢ Delegation is useful when creating classes that have a single responsibility and collaborate with other classes
to achieve that responsibility.
• In Java, an "is-a" relationship is a term used to describe the relationship between two classes, where one class is
a specialized version of the other. This relationship is also known as 1. inheritance or subclassing. In this
relationship, the subclass (or derived class) inherits properties and behaviors from its superclass (or base class).
• The "is-a" relationship is important in Java because it allows developers to reuse code and create more efficient
and flexible programs. By using inheritance, developers can create a hierarchy of classes that share common
properties and behaviors, while also allowing for specialization and customization in subclasses. This can help to
reduce code duplication, improve code readability and maintainability, and make it easier to add new features and
functionality to a program over time
1
9.2 | CODE REUSE COMPOSITION
• In Java, Has-A-Relation is a type of relationship between classes, which is used to define the ownership of one
object by another object. This relationship is also known as composition or aggregation.
• 2. Composition is a strong form of the Has-A-Relation, where one class owns the other class and is responsible for
its lifetime. If the owner class is destroyed, then the owned class is also destroyed. Composition represents a
"whole-part" or “containment" relationship between two classes, where one class is composed of one or more
objects of another class.
• In Java, Has-A-Relation is a type of relationship between classes, which is used to define the ownership of one
object by another object. This relationship is also known as composition or aggregation.
• 2. Aggregation in Java is a type of relationship between two classes where one class, the aggregate class, has a
reference to the other class, the part class. The aggregate class is considered to own the part class.
2
9.4 | CODE REUSE DELEGATION
• 3. Delegation is a mechanism that allows a class to delegate responsibility for a task to another class. In this
approach, a class delegates the implementation of a method to another object that is responsible for providing the
implementation. This enables the class to reuse the functionality of the delegate object without having to implement
it itself.
• Delegation is useful when creating classes that have a single responsibility and collaborate with other classes to
achieve that responsibility
3
OOP– FT | MOD10: INTERFACE
10.0 | INTERFACE
• An interface in Java is a blueprint of a class. It has static constants and abstract methods..
• The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java
interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
• An interface is a reference type, similar to a class, that can contain only abstract methods (methods without a
body) and constants (final variables).
• Unlike classes, interfaces cannot store state (variable) and can only declare methods that a class must implement.
1. Interface provides a contract: It forces a class to follow a contract for method signatures, promoting consistency
across multiple implementations
2. Multiple inheritance: Java allows a class to implement multiple interfaces, solving the problem of multiple
inheritance (which Java does not allow with classes).
3. Loose coupling: Interfaces decouple the " from the " making the system more flexible and scalable.
• Attributes in an interface are implicitly public, static, and final. This means they are constants and cannot be
changed once they are initialized.
• Interfaces are used to declare constants that can be accessed across all implementations of that interface, and they
cannot store instance variables or mutable state.
4
10.3 | METHODS IN AN INTERFACE
1. Abstract Methods
➢ Methods in an interface were abstract, meaning they had no body and needed to be implemented by the class
that implements the interface.
➢ Abstract methods must be implemented by the implementing class.
2. Default Methods
➢ Interfaces can now have methods with default implementations using the default keyword. These methods can
be overridden by implementing classes if needed.
➢ Default methods provide optional behavior that can be overridden.
3. Static Methods
➢ Interfaces can also have static methods, which can be called using the interface name without needing an
instance.
➢ Static methods belong to the interface itself and are not tied to class instances.
• Inheritance using an interface allows a class to inherit the abstract methods of an interface and provide concrete
implementations for those methods. This enables a class to follow multiple contracts (interfaces) at once.
5
10.5 | INHERITANCE CHAIN IN INTERFACES
• One interface can extend another interface, meaning it can inherit the
abstract methods from the parent interface and add its own.