Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
2 1.Fundamental Principles of OOP 2.Inheritance ▪ Class Hierarchies ▪ Inheritance and Access Levels Table of Contents
3 sli.do #3666 Questions
Fundamental Principles of OOP
▪Encapsulation ▪Inheritance ▪ Inherit members from parent class ▪ Inherit constructors from parent class ▪ Polymorphism ▪ Abstraction Fundamental Principles of OOP 5
Inheritance
Types of Inheritance derived class base class / parent class inherits 7
Types of Inheritance (2) interface class implements 8
Types of Inheritance (3) derived interface base interface extends 9
1. Extensibility 2. Reusability 3. Provides abstraction Inheritance – Benefits 10
Don't use it to build has-a relationship E.g. dog has-a name (dog is not a name) Examples - Incorrect 11
Use inheritance for building is-a relationship E.g. dog is-a animal (dog is an animal) Examples - Correct 12
13 How to Define Inheritance? class DerivedClass extends BaseClass
14 Use super to invoke the parent constructor How to Define Inheritance? (2) public Circle(int x, int y) { super(x, y); }
Exercises in Class
16 Private members are hidden Inaccessible Members
17 Inheritance – Example Person +Name: string +Address: string Employee +Company: string +Salary: decimal Student +School: string Base class Derived class
Class Hierarchies Game MultiplePlayersGame BoardGame Chess Backgammon SinglePlayerGame Minesweeper Solitaire 18
Simple Inheritance Live Demo
20 ▪ public ▪ private ▪ protected ▪no modifier Access Modifiers in Java
21 There is no multiple inheritance Inheritance: Important Aspects
22 Constructors are not inherited Inheritance: Important Aspects (2)
23 Extended class could add new members, but couldn’t remove derived members Inheritance: Features
24 New members with the same name or signature hide the inherited ones Inheritance: Features (2)
Exercises in Class
26 UML class diagrams Inheritance Hierarchies
27 UML Class Diagram – Example
Class Diagrams in IntelliJ Idea Live Demo
29 ▪Inheritance allows extending the behavior of the classes ▪ Inheriting Fields ▪ Inheriting Methods ▪ Reusing the existing code Summary
? ? ? ? ? ? ? ? ? OOP – Encapsulation
License ▪ This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 31 ▪ Attribution: this work may contain portions from ▪ "Fundamentals of Computer Programming with Java" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with JavaCC-BY-SA ▪ "OOP" course by Telerik Academy under CC-BY-NC-SA licenseOOPCC-BY-NC-SA
Free Software University ▪ Software University Foundation – softuni.orgsoftuni.org ▪ Software University – High-Quality Education, Profession and Job for Software Developers ▪ softuni.bg softuni.bg ▪ Software Facebook ▪ facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity ▪ Software YouTube ▪ youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity ▪ Software University Forums – forum.softuni.bgforum.softuni.bg