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

MOD9-10-OOP-FT

The document discusses code reuse in object-oriented programming, highlighting three main mechanisms: inheritance, composition, and delegation. It explains how inheritance allows classes to share properties and methods, composition enables classes to contain instances of other classes, and delegation allows classes to delegate tasks to other classes. Additionally, it covers interfaces in Java, their purpose, attributes, methods, and the differences between abstract classes and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

MOD9-10-OOP-FT

The document discusses code reuse in object-oriented programming, highlighting three main mechanisms: inheritance, composition, and delegation. It explains how inheritance allows classes to share properties and methods, composition enables classes to contain instances of other classes, and delegation allows classes to delegate tasks to other classes. Additionally, it covers interfaces in Java, their purpose, attributes, methods, and the differences between abstract classes and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OOP– FT | MOD9: CODE REUSE, MAKING CHANGES IN CLASS HIERARCHY

9.0 | CODE REUSE

• 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.

There are several ways to achieve code reuse including:

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.

9.1 | CODE REUSE INHERITANCE

• 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.

9.3 | CODE REUSE AGGREGATION

• 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.

Imagine you are developing an application for different types of vehicles


(cars, bikes, trucks). Each type of vehicle has some common actions like "start”
and “stop,” but the way each vehicle performs these actions varies. Using
interfaces, you can define the structure without implementation and let each
vehicle provide its specific implementation.

10.1 | WHY USE INTERFACES?

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.

10.2 | ATTRIBUTES IN AN INTERFACE

• 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.

10.4 | INHERITANCE USING INTERFACE

• 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.

10.6 | ABSTRACT CLASS VS. INTERFACE

Feature Abstract Class Interface


A class that cannot be instantiated and can A completely abstract structure that only contains
Definition
contain both abstract and concrete methods. method signatures (before Java 8).
Prior to Java 8, only abstract methods. From Java 8
Can have both abstract (without body) and
Methods onwards, can have default and static methods with a
concrete (with body) methods.
body.
Fiels Can have instance variables (fields) and Cannot have instance variables, only constants
(Attributes) constants. (implicitly public, static, and final)
Constructors Can have constructors. Cannot have constructors.
A class can implement multiple interfaces (provides
Inheritance A class can extend only one abstract class.
multiple inheritance)
Access Can have any access modifier (private, Methods are implicitly public and abstract (prior to
Modifiers protected, public). Java 8).
Used when classes share a common base but Used to define a contract that multiple classes can
Use Case
may also share implemented code. implement in their own way.
More restrictive since a class can only
Flexibility More flexible due to multiple inheritance support.
extend one abstract class.

10.7 | WHEN TO USE ABSTRACT CLASS VS. INTERFACE

Use an abstract class when:

• You want to share common code among closely related classes.


• You want to provide a base class with default implementations and common fields.

Use an interface when:

• You expect classes to have unrelated implementations.


• You want to ensure a class follows a contract but don’t want to impose an inheritance structure.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy