Download presentation
Presentation is loading. Please wait.
Published byEdmund West Modified over 9 years ago
1
Chapter 10: Inheritance 1
2
Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass. As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined for the parent class 2
3
Inheritance To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones Software reuse is at the heart of inheritance By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 3
4
Inheritance Inheritance relationships often are shown graphically in a UML class diagram, with an arrow with an open arrowhead pointing to the parent class 4 Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Person Employee
5
Deriving Subclasses In Java, we use the reserved word extends to establish an inheritance relationship 5 class Employee extends Person { // class contents }
6
Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another class. The subclass inherits all data attributes of its superclass The subclass inherits all methods of its superclass The subclass inherits all associations of its superclass The subclass can: Add new functionality Use inherited functionality Override inherited functionality Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date superclass: subclass: 6
7
Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date public class Person { private String name; private Date dob; [...] public class Employee extends Person { private int employeID; private int salary; private Date startDate; [...] Employee anEmployee = new Employee(); 7
8
Each Java class has one (and only one) superclass. Inheritance creates a class hierarchy Classes higher in the hierarchy are more general and more abstract Classes lower in the hierarchy are more specific and concrete Class There is no limit to the number of subclasses a class can have There is no limit to the depth of the class tree. Class 8
9
The super Reference Constructors are not inherited, even though they have public visibility Yet we often want to use the parent's constructor to set up the "parent's part" of the object The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor 9
10
The super Reference A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor The super reference can also be used to reference other variables and methods defined in the parent’s class 10
11
Constructors - Example public class BankAccount { private String ownersName; private int accountNumber; private float balance; public BankAccount(int anAccountNumber, String aName) { accountNumber = anAccountNumber; ownersName = aName; } [...] } public class OverdraftAccount extends BankAccount { private float overdraftLimit; public OverdraftAccount(int anAccountNumber, String aName, float aLimit) { super(anAccountNumber, aName); overdraftLimit = aLimit; } 11
12
The protected Modifier Visibility modifiers determine which class members are inherited and which are not Variables and methods declared with public visibility are inherited; those with private visibility are not But public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected 12
13
The protected Modifier The protected modifier allows a member of a base class to be inherited into a child Protected visibility provides more encapsulation than public visibility does However, protected visibility is not as tightly encapsulated as private visibility Protected variables and methods can be shown with a # symbol preceding them in UML diagrams 13
14
14 Protected Visibility for Superclass Data private data are not accessible to subclasses! protected data fields accessible in subclasses
15
UML Diagram for Words Book # pages : int + pageMessage() : void Dictionary - definitions : int + definitionMessage() : void 15
16
Protected visibility public class Person { private String name; protected String LastName; Public int age; } public class Employee extends Person { public void Setter () { name = “Sara”; LastName = “Ali”; Age = 20; } Employee anEmployee = new Employee(); System.out.print(anEmplyee.name); System.out.print(anEmplyee.LastName); System.out.print(anEmplyee.age); 16
17
Multiple Inheritance Java supports single inheritance, meaning that a derived class can have only one parent class Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents Java does not support multiple inheritance 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.