Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is inheritance? It is the ability to create a new class from an existing class.

Similar presentations


Presentation on theme: "What is inheritance? It is the ability to create a new class from an existing class."— Presentation transcript:

1 What is inheritance? It is the ability to create a new class from an existing class.

2 Inheritance  The objectives of this chapter are:  To explore the concept and implications of inheritance  Polymorphism  To define the syntax of inheritance in Java  To understand the class hierarchy of Java  To examine the effect of inheritance on constructors

3 Terminology  Inheritance is a fundamental Object Oriented concept  All classes can be defined as a sub class of another class  The super class is the more general class (few details)  The sub class is the more specific class (many details) Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date superclass: subclass:

4  At the very top of the inheritance hierarchy is a class called Object  All Java classes inherit from Object  Every class without the extends keyword automatically extends the class Object  The Object class is defined in the java.lang package The Object Class

5  All methods defined in the class Object are available in every class including  equals(otherObject)  Compares two objects for equality, which is defined in Object as true when two object references point to the same object (x==y)  toString()  Returns a string representation of the object  Both of these methods should be overridden in a child class so they are defined more precisely for that class The Object Class

6 Types of Inheritance Single Inheritance A child can only have one parent Multiple Inheritance A child can inherit from multiple parents Multiple inheritance is not permitted in Java Single inheritance can be limiting – for example, Person can be a Student or Employee but not both

7 Super Class and Sub Class  A superclass, parent class, base class is a generalized class  A subclass, child class, derived class is a more specific, detailed version of a superclass

8 Inheritance in Java Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object 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();

9 Inheritance in Java  Each class has only one parent  Inheritance creates a class hierarchy  No limit to the number of subclasses a class can have  No limit to the depth of the class hierarchy Class

10 IS-A Relationship Use the extends keyword to establish an inheritance relationship  Sub extends Super public class Animal { } public class Cat extends Animal { } public class Tiger extends Cat { } Cat is-a Animal Tiger is-a Cat Tiger is-a Animal as well These are “siblings”

11 Inherited by sub class:  Variables of the super class with public or protected visibility  Methods of the super class with public or protected visibility NOT inherited by sub class:  Constructors of the super class  Variables and methods of the super class with private visibility  Use public accessor and modifier methods to access the private members of the super class What Exactly is Inherited?

12 public class ClassA { public int x; private int y; public int z; … } public class ClassB extends ClassA { public int a; private int b; … } public class ClassC extends ClassB { private int q; … } What instance data is accessible in ClassA? ClassB? ClassC? x, y, z a, b, x, z q, a, x, z

13  A sub class is a more specialized type of the super class  A child class has to provide its own constructor(s)  The first statement in the child class constructor is a call to super() or super(parameter list) to set up the “parent part” of the object  If you don’t call super() explicitly, it will automatically be done anyway  You cannot call the superclass constructor from any other method super( )

14  The constructor of the superclass must execute first, then the constructor of the subclass Constructors public class Animal { private String name; public Animal(String name) { this.name = name; } public class Dog extends Animal { public String breed; public Dog(String name, String breed) { super(name); this.breed = breed; } Call to the parent class super(…) must be the first line in the sub class constructor

15  Constructors are never inherited!  If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass  If you don’t have a no-arg constructor, but do have a constructor with parameters, a compiler error will occur  Make it a habit to include a no-arg constructor for classes you write to avoid this Constructors and super

16 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; } Constructor Example This is the child class First line of the child class constructor is the call to super

17  When an object reference variable of a superclass is declared, it can point to objects of its class or objects of any of its subclasses  Legal declarations: Animal a = new Animal(“Alex”); Animal b = new Dog(“Bo”, “collie”);  You cannot use a subclass reference for an object of the superclass type! Dog d = new Animal(“Spot”);  invalid! Constructing Subclass Objects

18  If one class extends another which extends another, you can imagine a whole chain of constructors going all the way back to Object  This is called constructor chaining Constructor Chaining

19 Class Hierarchy UML notation is used to show relationship between classes Open arrow points up at the super class from a subclass

20 UML Notation - Class Team - teamName: String - playerList: ArrayList - numPlayers: int + Team + addPlayer: boolean + outputTeamDetails: String Name of class (italics if an abstract class) Attributes of the class -- name : return type Constructor and methods of the class The plus (+) and minus (-) signs indicate if the member is public or private. You may also see a hash (#) sign which indicates the member is protected (private to all but subclasses).

21 UML Notation - Other Interface relationships: Unfilled arrow, dashed line Association relationship: Solid arrow/line shows 1 Customer may have n Orders > at top

22 A sub class can …  Use the inherited variables and methods of the super class (public and protected) just as they are  Have its own constructor(s) that invokes the constructor of the superclass, explicitly or implicitly  Add new variables and methods – subs are more detailed, remember!  Override an inherited method from super, perhaps to make it more detailed (but can’t override static methods of super!)  Declare a field with the same name as in the superclass, hiding it  Declare a static method with the same name as in the superclass, hiding it What a Subclass Can Do

23 Method overriding is different than method overloading  Method overriding – A method in a subclass has the exact same method signature as the method in the super class  Method overloading – multiple methods in the same class have the same name but different method signatures (for example, the println method) Method Overriding vs. Overloading

24  Method overriding – A method in a subclass has the exact same method signature as the method in the super class  Methods that should be overridden in subclasses toString() equals(Object other)  Methods that cannot be overridden by a subclass  private methods – not accessible to the child classes  final methods – using the “final” keyword to prevents a subclass from altering a method that is critical to the functioning of the class Overriding Superclass Methods

25  The best way to redefine a method is to reuse the parent class’ method and supplement it with the code needed to handle the changes in the subclass  If a subclass overrides a method that is also in the parent class, the subclass version of the method is the version that will be executed Method Overriding

26  A polymorphic method is a method that has been overridden in at least one subclass  Polymorphism – the mechanism of selecting the appropriate method for a particular object in a class hierarchy  In Java the correct method is chose by the type of the actual object, not the type of the object reference Polymorphism

27  If both the superclass and a subclass both have a method with the same name and method signature, that method is polymorphic  Say the Animal class has a method speak() and so does the Dog class which extends Animal Animal a = new Animal(); a.speak(); //displays “a noise” Animal b = new Dog(); b.speak(); //displays “bark” Polymorphic Method Calls At runtime the correct version of the method is determined


Download ppt "What is inheritance? It is the ability to create a new class from an existing class."

Similar presentations


Ads by Google