Understanding Inheritance in Object-Oriented Programming

Slide Note
Embed
Share

Inheritance in Java is a key concept where a subclass can inherit properties and behaviors from a superclass. This mechanism allows for hierarchical organization and the creation of modular classifications. The relationship between superclasses and subclasses is crucial in defining object-oriented designs and structures.


Uploaded on Aug 04, 2024 | 4 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. 6001104-3 Structured Programming L. Rafika Maaroufi 1

  2. Chapter6: Inheritance 2

  3. Objectives The notions of superclasses and subclasses and the relationship between them. To use keyword extends to create a class that inherits attributes and behaviors from another class. 3

  4. Outline Inheritance in java Superclasses and Subclasses Relationship between Superclasses and Subclasses 4

  5. Inheritance in java Inheritance in java Inheritance in java : is a mechanism in which one object acquires all the properties and behaviors of a parent object is one of the object-oriented programming (OOP) primary capabilities. is a mechanism for a modular and hierarchical organization allows the creation of hierarchical classifications 5

  6. A hierarchical design A hierarchy involving architectural buildings 6

  7. Superclasses and Subclasses In object-oriented terminology: the existing class is typically described as the base class, parent class, or superclass. the newly defined class is known as the subclass or child class. 7

  8. Superclasses and Subclasses Inheritance examples: 8

  9. Superclasses and Subclasses The subclass can differentiate itself from its superclass in two ways: It may augment the superclass by adding new fields and new methods. It may also specialize existing behaviors by providing a new implementation that overrides an existing method. 9

  10. Relationship between Superclasses and Subclasses is a is a relationship : is a correspondence between levels Examples: (1) a house is a building Superclass: building Subclass: house (2) a ranch is a house. Superclass: house Subclass: ranch 10

  11. The extends keyword Syntax of java Inheritance: class Subclass-name extends Superclass-name { //methods and fields } The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. We say that the subclass extends the superclass. 11

  12. Java Inheritance Example In Java The relationship between the two classes is: Programmer IS-A Employee Employee Salary: float Programmer bonus: int 12

  13. Why use inheritance in java? Method Overriding: If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, it is used to provide the specific implementation of a method which is already provided by its superclass Reusability: is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. 13

  14. Example: Java Program to demonstrate why we need method overriding calling the method of parent class with child class object 14

  15. Types of inheritance in java There can be three types of inheritance in java: Class A Class A Class A Class B Class B Class B Class C single single hierarchical hierarchical Class C multilevel multilevel 15

  16. Single Inheritance Example I 16

  17. Multilevel Inheritance Example I 17

  18. Hierarchical Inheritance Example I 18

  19. References Java: How to Program, 9e, Dietel and Dietel, Pearson 0273759760 Data Structures and Algorithms in Java, 6th edition, by M.T. Goodrich and R. Tamassia. John Wiley and Sons, Inc., ISBN: 0-471-73884-0 Java The Complete Reference Ninth Edition 19

Related


More Related Content