PDF Google Drive Downloader v1.1


Báo lỗi sự cố

Nội dung text Day 2 - OOPs Inheritance.pdf

● ● ● ● ● ● ● ● ● Day 2 - OOPs Inheritance ITSRUNTYM What is Inheritance? Inheritance is one of the four pillars of Object-Oriented Programming (OOP) (along with Abstraction, Encapsulation, and Polymorphism). Definition: Inheritance is the process where one class acquires the properties and behaviors (fields and methods) of another class. Why Use Inheritance? Code Reusability: Avoid code duplication Logical Hierarchy: Model real-world relationships (Car IS-A Vehicle) Extensibility: Easily add new features without modifying old code Polymorphism: Enables method overriding and dynamic dispatch Disadvantages of Inheritance Tight coupling Overriding might break base functionality Can lead to fragile base class problem Real-Life Analogy: Imagine: Vehicle is a general category (base class). Car, Bike, and Bus are specific types of vehicles (subclasses). They all have common features like start(), stop() which should not be rewritten every time. Basic Syntax: class Parent { void display() {

● ● ● ● ● ● ● } Problem: Car is dependent on the concrete class Engine If we change the Engine class, Car must be changed too Violates: Open/Closed Principle Dependency Inversion Principle Loose Coupling (GOOD practice) Definition: Loose coupling means reducing dependencies between components so that changes in one do not affect others. Achieved via: Interfaces Dependency Injection (DI) Inversion of Control (IoC) Code Example: interface Engine { void start(); } class PetrolEngine implements Engine { public void start() { System.out.println("Petrol Engine Started"); } } class Car { private Engine engine; // Constructor Injection (Dependency Injection) Car(Engine engine) { this.engine = engine; } public void startCar() { engine.start();

Tài liệu liên quan

x
Báo cáo lỗi download
Nội dung báo cáo



Chất lượng file Download bị lỗi:
Họ tên:
Email:
Bình luận
Trong quá trình tải gặp lỗi, sự cố,.. hoặc có thắc mắc gì vui lòng để lại bình luận dưới đây. Xin cảm ơn.