Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.

Similar presentations


Presentation on theme: "Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or."— Presentation transcript:

1 Inheritance 1

2  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 Superclasses and subclasses  Superclasses and subclasses  Object of one class “is an” object of another class  Example: lexus is car.  Class lexus inherits from class car  car : superclass  lexus : subclass  Superclass typically represents larger set of objects than subclasses  Example:  superclass: Vehicle  Cars, trucks, boats, bicycles, …  subclass: Car  Smaller, more-specific subset of vehicles 5

6 Examples 6

7 7 Fig. 9.2 | Inheritance hierarchy for university CommunityMembers

8 Examples 8 Fig. 9.3 | Inheritance hierarchy for Shapes.

9 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 9

10 Deriving Subclasses  In Java, we use the reserved word extends to establish an inheritance relationship 10 class Employee extends Person { // class contents }

11 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: 11

12 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(); 12

13 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 13

14 14 Suppose we want implement a class Employee which has two attributes, id and name, and some basic get- and set- methods for the attributes. We want now define a PartTimeEmployee class; this class will inherit these attributes and methods, but can also have attributes (hourlyPay) and methods of its own (calculateWeeklyPay). Case Study 1:

15 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 15

16 The super Reference  A child’s constructor is responsible for calling the parent’s constructor  The first line  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 public class SubClass extends SuperClass { public SubClass(………….) { super (………); //DATA MEMBERS ……. } } 16

17 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; } 17

18 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 18

19 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 19

20 20 Protected Visibility for Superclass Data  private data are not accessible to subclasses!  protected data fields accessible in subclasses

21 UML Diagram for Words 21 Person - name: String #lastname : string + age : int Employee - employeeID: int - salary: int - startDate: Date

22 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); 22 Clint class

23 Inheritance and Member Accessibility  We use the following visual representation of inheritance to illustrate data member accessibility. 23

24 The Effect of Three Visibility Modifiers 24

25 Accessibility of Super from Sub  Everything except the private members of the Super class is visible from a method of the Sub class. 25

26 Accessibility from Unrelated Class class Client { public void main (String [] args) { Super mySuper = new Super(); Sub mySub = new Sub(); int i = mySuper.pub_Super_Field; int j = mySub.pub_Super_Field; // inherited int k = mySub.pub_Sub_Field; } VALID  class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super () { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub () { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } :Super :Sub :Client

27 Accessibility from Unrelated Class class Client { public void test() { Super mySuper = new Super(); Sub mySub = new Sub(); int l = mySuper.pri_Super_Field; int m = mySub.pri_Sub_Field; int n = mySub.pri_Super_Field; } class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } NOT VALID  :Super :Sub :Client

28 Accessibility from Unrelated Class class Client { public void test() { Super mySuper = new Super(); Sub mySub = new Sub(); int o = mySuper.pro_Super_Field; int p = mySub.pro_Sub_Field; int q = mySub.pro_Super_Field; } class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } NOT VALID  :Super :Sub :Client

29 Accessibility of Super from Super class Super { public void superToSuper(Super anotherSuper) { int i = anotherSuper.pub_Super_Field; int j = anotherSuper.pro_Super_Field; int k = anotherSuper.pri_Super_Field; } VALID  :Super anotherSuper

30 Accessibility of Sub from Sub class Sub extends Super { public void subToSub(Sub anotherSub) { int i = anotherSub.pub_Sub_Field; int j = anotherSub.pro_Sub_Field; int k = anotherSub.pri_Sub_Field; int l = anotherSub.pub_Super_Field; //inherited int m = anotherSub.pro_Super_Field; int n = anotherSub.pri_Super_Field; } VALID  NOT VALID   :Sub anotherSub

31 Accessibility of Sub from Super class Super { public void superToSub(Sub sub) { int i = sub.pub_Sub_Field; int j = sub.pro_Sub_Field; int k = sub.pri_Sub_Field; } VALID  NOT VALID  :Super :Sub sub

32 Accessibility of Super from Sub 4 th Ed Cha pter 2 - 32 class Sub extends Super { public void subToSuper(Super mySuper) { int i = mySuper.pub_Super_Field; int j = mySuper.pro_Super_Field; int k = mySuper.pri_Super_Field; } VALID  NOT VALID  :Sub :Super mySuper


Download ppt "Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or."

Similar presentations


Ads by Google