OOP D.2 Features of OOP
Features of OOP
Encapsulation
Definition: Encapsulation is the practice of hiding the inner design of an object data type in a class.
Implementation in Java: Achieved by using the private keyword for fields and methods that should not be accessible outside the class.
Concept of Data Hiding: Limits access to the internals of the object, often termed as data hiding.
Separation of Concerns: Distinguishes between implementation (how it works) and interface (how it is used).
User Focus: Users can focus on using the object without worrying about internal complexities.
Data Integrity: Protects internal data from being accessed or corrupted by other objects.
Decoupling: Changes in implementation do not affect the interface, thereby reducing dependencies.
Real-World Analogy: The workings of a car are hidden (implementation) while the controls (interface) are user-friendly.
Inheritance
Definition: Inheritance allows one class (subclass) to inherit fields and methods from another class (superclass).
Mechanics: Subclass gains access to all the superclass properties (except private ones).
Overriding: Subclass can override superclass methods, creating its own version.
Relationship: Forms an "is-a" relationship, e.g., a Car class inherits from a Vehicle class.
Advantages:
Code Reuse: Reuse existing code to create new object types.
Error Prevention: Reduces duplicate code, minimizing errors.
Java Implementation: Achieved using the extends keyword.
Types of Inheritance in Java
Single Inheritance: A subclass inherits from only one superclass.
Multilevel Inheritance: A class inherits from a class that is a subclass of another class (chain of inheritance).
Hierarchical Inheritance: A single superclass is inherited by multiple subclasses.
Examples of Inheritance
Single Inheritance Example:
class Animal { void eat() { System.out.println("eating..."); }} class Dog extends Animal { void bark() { System.out.println("barking..."); }} public class DemoInheritance { public static void main(String[] args) { Dog d = new Dog(); d.bark(); d.eat(); }}Multilevel Inheritance Example:
class Animal1 { void eat() { System.out.println("eating..."); }} class Dog1 extends Animal1 { void bark() { System.out.println("barking..."); }} class BabyDog extends Dog1 { void weep() { System.out.println("weeping..."); }} public class DemoMultilevelInheritance { public static void main(String[] args) { BabyDog d = new BabyDog(); d.weep(); d.bark(); d.eat(); }}Hierarchical Inheritance Example:
class Animal2 { void eat() { System.out.println("eating..."); }} class Dog2 extends Animal2 { void bark() { System.out.println("barking..."); }} class Cat extends Animal2 { void meow() { System.out.println("meowing..."); }} public class DemoHierarchicalInheritance { public static void main(String[] args) { Cat c = new Cat(); c.meow(); c.eat(); }}
Activity: Inheritance Hierarchy Practice
Sample Superclasses:
Vehicle {Car, Bus, Lorry}
Animal {…etc}
Employee
RPGCharacter
Shape
Publication
Subject
Teacher
Student
Task:
Choose a superclass.
Identify three possible subclasses.
Identify one variable and one method for the superclass.
Identify one variable and one method for each subclass.
Is-a Test: Ensure the hierarchy follows the format "A [Subclass] is a [Superclass]".
Activity: Animal Hierarchy
Classes:
Animal:
Attributes: name: String, age: int
Methods: eat(): Outputs "Animal is eating."
Bird (inherits from Animal):
Additional Attributes: wingSpan: double
Methods: eat(): Outputs "Bird is eating."
Fish (inherits from Animal):
Additional Attributes: finCount: int
Methods: eat(): Outputs "Fish is eating."
Tasks:
Part A: Draw a UML diagram.
Part B: Create Java program to instantiate Bird and Fish and call eat() method.
Polymorphism
Definition: Refers to one object or function exhibiting different attributes and behaviors depending on context.
Method Overloading: Allows multiple methods to have the same name but different parameter lists.
Java’s Method Selection: Java determines which method to use based on argument types and counts.
Benefits:
Robustness: Objects can handle a variety of input types.
Code Simplification: Reduces need for explicit handling of different scenarios.
Advantages of Libraries of Objects
Definition: A library is a collection of pre-written code that can be reused.
Benefits:
Code Reusability: Reduces need for redundant code.
Performance: Library code is often optimized by experienced programmers.
Reliability: Thoroughly tested library code improves software reliability.
Example: Java API provides a comprehensive library for developers.
Disadvantages of OOP
Complexity: Simple tasks can be over-complicated through OOP practices.
Learning Curve: Key OOP concepts like inheritance, encapsulation, and polymorphism can be difficult to grasp initially.
Specialist Skills: May require different programmers for different tasks (e.g., testing, documentation).
Advantages of Programming Teams**
Efficiency: Specialists can work on different areas (e.g., testing) concurrently.
Reduced Development Time: Parallel development decreases time to build software.
Complexity Management: Independent programmers can work without interfacing issues, allowing for a cleaner development process.
Advantages of Modularity in Program Development
Definition: Involves breaking down the program into modules (functions, classes).
Collaboration: Teams can work on separate modules concurrently.
Understandability: Systems are easier to comprehend when divided into modules.
Code Reuse: Modules can be repurposed across multiple systems.
Testing: Facilitates unit testing as modules can be tested independently.