Submitted to Ms. Raiyan Janik Monir Lecturer Department of SWE Daffodil International University Inharitance Object Oriented Programming In
Section : A ID : 232-35-292 Rukaiya Akter Trisha
Why Use Inheritance? Inheritance lets you reuse code from existing classes, so we don’t need to rewrite common functionality. Code Reusability: Easier Maintenance: Changes in the superclass automatically apply to subclasses, making updates easier. Organized Structure: It creates a logical hierarchy, making code easier to understand and maintain. Extensibility: We can add new features to subclasses without changing existing code in the superclass.
Section : A ID : 232-35-003 Md. Abdullah Al Noman
Key Concepts of Inharitance The class from which other classes inherit properties and methods. In th Picture parent Class is Animal. Declare Parent Class in Java: class Animal { } Parent Class Child Class Which inherits from the Parent Class and can add or override methods and properties. In th Picture Child Classes are Dog, Cat, Cow. Declare Child Class in Java: class Dog extands Animal { } class Cat extands Animal { } class Cow extands Animal { } Parent Class Child Class Extend Keyword extends used to create a Child class establishing an inheritance relationship with Parent class
Public Private Protected Default public (+) members are accessible from any other class. private (-) members are accessible only within the declared class. protected (#) members are accessible within the same package and subclasses. Default members (no modifier) are accessible only within the same package.
Section : A ID : 232-35-016 Reduan Ahmad
Code Reusability Simpler Code Structure Easier Maintenance Supports Polymorphism We can reuse the same code again and again. This makes the code structure simple, easier to maintain, and flexible. One of the major advantages is that inheritance also supports polymorphism.
Parent Class (Animal): This has common features of all animals. Child Class (Dog): This class inherits from Animal and can add extra features specific to dogs. // Parent classclass Animal { void eat() { System.out.println("Animal is eating"); } } // Child classclass Dog extends Animal { void bark() { System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Dog myDog = new Dog(); // Uses method from parent class(Animal) myDog.eat(); // Uses method from child class (Dog) myDog.bark(); } }
Section : A ID : 232-35-214 Asraful Alam
Method Overriding Declaring a method in a subclass which is already present in the superclass is known as Method Overriding. Example: class Student{ String name; void displayInfo() { System.out.println("Name : "+name); } } class Teacher extends Student{ int age; void displayInfo() { System.out.println("Name : "+name); System.out.println("Age : "+age); } }
public class test{ public static void main(String[] args) { Teacher t1 = new Teacher(); t1.name = "Mehedi"; t1.age = 25; t1.displayInfo(); Student s1 = new Student(); s1.name= "Nakib"; s1.displayInfo(); } } Example:
Section : A ID : 232-35-024 Adnan Iqbal
Types Of Inheritance : 01.Single Inheritance 02.Multilevel Inheritance 03.Hierarchical Inheritance
Single inheritance: Code: class Animal { void sound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); dog.bark(); } } Explanation: Here, Dog inherits from Animal, so it gets the sound() method for free. This is Single Inheritance.
Code: class Animal { void sound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } class Puppy extends Dog { void cry() { System.out.println("Puppy cries"); } } Multilevel Inheritance: Explanation: Puppy inherits from Dog, and Dog inherits from Animal. Puppy can use both bark() and sound() methods.
Code: class Animal { void sound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } class Cat extends Animal { void meow() { System.out.println("Cat meows"); } } Hierarchical Inheritance: Explanation: Both Dog and Cat inherit from Animal, so they both have access to sound().
Section : A ID : 232-35-066 Rashedul Islam
Real-Life Application: class Person { String name; int age; } class Student extends Person { String studentId; } class Teacher extends Person { String employeeId; } Code: Education System E-commerce System Code: class Product { String name; double price; } class Electronics extends Product { String warrantyPeriod; } class Clothing extends Product { String size; String material; } class Books extends Product { String author; }
By placing common methods and properties in a base class, child classes can reuse this code without having to rewrite it. This avoids duplication and reduces errors. 1. Code Reusability 3. Easy Maintenance Since the shared code is in one place, changes to common behaviors or attributes can be made in the base class, and these changes will automatically apply to all derived classes. 2. Logical Organization Inheritance allows a logical hierarchical structuring of related classes. It makes the code more intuitive and easier to follow. 4. Scalability Inheritance supports the growth of applications by making it easy to add new types without disrupting existing functionality.
Disadvantage: Changes in the base class can unintentionally affect all derived classes, which may introduce bugs or require changes in multiple places. 1. Tight Coupling 3. Overhead Subclasses may inherit methods and properties that are unnecessary, leading to inefficient memory use and potentially slower performance. 2. Complexity Deep inheritance hierarchies can make the codebase more complex and harder to understand and maintain. 4. Rigidity Inheritance hierarchies can be rigid. All subclasses must follow the structure defined by their parent classes, which might not always fit the specific requirements.
Everest Cantu Thank You!😃

Presentation Slide about Inharitance in Java Object Oriented Programming

  • 1.
    Submitted to Ms. RaiyanJanik Monir Lecturer Department of SWE Daffodil International University Inharitance Object Oriented Programming In
  • 2.
    Section : A ID: 232-35-292 Rukaiya Akter Trisha
  • 5.
    Why Use Inheritance? Inheritancelets you reuse code from existing classes, so we don’t need to rewrite common functionality. Code Reusability: Easier Maintenance: Changes in the superclass automatically apply to subclasses, making updates easier. Organized Structure: It creates a logical hierarchy, making code easier to understand and maintain. Extensibility: We can add new features to subclasses without changing existing code in the superclass.
  • 6.
    Section : A ID: 232-35-003 Md. Abdullah Al Noman
  • 7.
    Key Concepts ofInharitance The class from which other classes inherit properties and methods. In th Picture parent Class is Animal. Declare Parent Class in Java: class Animal { } Parent Class Child Class Which inherits from the Parent Class and can add or override methods and properties. In th Picture Child Classes are Dog, Cat, Cow. Declare Child Class in Java: class Dog extands Animal { } class Cat extands Animal { } class Cow extands Animal { } Parent Class Child Class Extend Keyword extends used to create a Child class establishing an inheritance relationship with Parent class
  • 8.
    Public Private Protected Default public (+) membersare accessible from any other class. private (-) members are accessible only within the declared class. protected (#) members are accessible within the same package and subclasses. Default members (no modifier) are accessible only within the same package.
  • 9.
    Section : A ID: 232-35-016 Reduan Ahmad
  • 10.
    Code Reusability SimplerCode Structure Easier Maintenance Supports Polymorphism We can reuse the same code again and again. This makes the code structure simple, easier to maintain, and flexible. One of the major advantages is that inheritance also supports polymorphism.
  • 11.
    Parent Class (Animal):This has common features of all animals. Child Class (Dog): This class inherits from Animal and can add extra features specific to dogs. // Parent classclass Animal { void eat() { System.out.println("Animal is eating"); } } // Child classclass Dog extends Animal { void bark() { System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Dog myDog = new Dog(); // Uses method from parent class(Animal) myDog.eat(); // Uses method from child class (Dog) myDog.bark(); } }
  • 12.
    Section : A ID: 232-35-214 Asraful Alam
  • 13.
    Method Overriding Declaring amethod in a subclass which is already present in the superclass is known as Method Overriding. Example: class Student{ String name; void displayInfo() { System.out.println("Name : "+name); } } class Teacher extends Student{ int age; void displayInfo() { System.out.println("Name : "+name); System.out.println("Age : "+age); } }
  • 14.
    public class test{ publicstatic void main(String[] args) { Teacher t1 = new Teacher(); t1.name = "Mehedi"; t1.age = 25; t1.displayInfo(); Student s1 = new Student(); s1.name= "Nakib"; s1.displayInfo(); } } Example:
  • 15.
    Section : A ID: 232-35-024 Adnan Iqbal
  • 16.
    Types Of Inheritance: 01.Single Inheritance 02.Multilevel Inheritance 03.Hierarchical Inheritance
  • 17.
    Single inheritance: Code: class Animal{ void sound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); dog.bark(); } } Explanation: Here, Dog inherits from Animal, so it gets the sound() method for free. This is Single Inheritance.
  • 18.
    Code: class Animal { voidsound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } class Puppy extends Dog { void cry() { System.out.println("Puppy cries"); } } Multilevel Inheritance: Explanation: Puppy inherits from Dog, and Dog inherits from Animal. Puppy can use both bark() and sound() methods.
  • 19.
    Code: class Animal { voidsound() { System.out.println("Animals make sounds"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks"); } } class Cat extends Animal { void meow() { System.out.println("Cat meows"); } } Hierarchical Inheritance: Explanation: Both Dog and Cat inherit from Animal, so they both have access to sound().
  • 20.
    Section : A ID: 232-35-066 Rashedul Islam
  • 21.
    Real-Life Application: class Person{ String name; int age; } class Student extends Person { String studentId; } class Teacher extends Person { String employeeId; } Code: Education System E-commerce System Code: class Product { String name; double price; } class Electronics extends Product { String warrantyPeriod; } class Clothing extends Product { String size; String material; } class Books extends Product { String author; }
  • 22.
    By placing commonmethods and properties in a base class, child classes can reuse this code without having to rewrite it. This avoids duplication and reduces errors. 1. Code Reusability 3. Easy Maintenance Since the shared code is in one place, changes to common behaviors or attributes can be made in the base class, and these changes will automatically apply to all derived classes. 2. Logical Organization Inheritance allows a logical hierarchical structuring of related classes. It makes the code more intuitive and easier to follow. 4. Scalability Inheritance supports the growth of applications by making it easy to add new types without disrupting existing functionality.
  • 23.
    Disadvantage: Changes in thebase class can unintentionally affect all derived classes, which may introduce bugs or require changes in multiple places. 1. Tight Coupling 3. Overhead Subclasses may inherit methods and properties that are unnecessary, leading to inefficient memory use and potentially slower performance. 2. Complexity Deep inheritance hierarchies can make the codebase more complex and harder to understand and maintain. 4. Rigidity Inheritance hierarchies can be rigid. All subclasses must follow the structure defined by their parent classes, which might not always fit the specific requirements.
  • 24.