Download presentation
Presentation is loading. Please wait.
Published bySurya Kusnadi Modified over 6 years ago
1
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Fall 2018 CISC124 11/19/2018 CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm. Quiz 2 grading is underway. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
2
Today Inheritance, Cont. Fall 2018 CISC124 - Prof. McLeod
3
Fall 2018 CISC124 Inheritance An OOP design technique that allows you to add to, modify, replace or simply adopt the methods and attributes in an existing class. The class that you are modifying is called the parent or super class, and the resulting class is called the child or sub class. The child or sub-class inherits all the public attributes and methods of the parent or superclass. The child class extends the parent class. The sub-class is always more concrete or less abstract than the super class. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
4
Why Bother? One reason is code re-use.
It is also a design tool that allows the coder to more easily model a real-life structure. You can also use inheritance to enforce code structure on child objects (using interfaces and abstract classes). And it is part of the polymorphism mechanism. Many classes in the Java API are intended for use as parent classes. Fall 2018 CISC124 - Prof. McLeod
5
Real Life Hierarchies Can you think of any? Flora, Fauna
Rocks & Minerals Food Menus Fasteners Vehicles Computers etc! Fall 2018 CISC124 - Prof. McLeod
6
https://en.wikipedia.org/wiki/List_of_popular_music_genres
Music Hierarchy For example, see: Almost everything must belong to a hierarchy. We often need to model real-world hierarchies in code. Easier to do because we already have the model. Fall 2018 CISC124 - Prof. McLeod
7
Example Our classroom: Person Professor Student
Both Professor and Student are sub-classes (or children) of the super-class or (parent) Person. Fall 2018 CISC124 - Prof. McLeod
8
Example, Cont. Person Professor Student Note that:
Professor “is a” Person. Student “is a” Person. Professor “is not a” Student. Student “is not a” Professor. Fall 2018 CISC124 - Prof. McLeod
9
Example, Cont. Child Objects always have an “is a” relationship with the Parent Object. In the Person class you would code all the attributes (age, gender, hair colour, etc.) that describe a person. The sub-classes only need to hold those attributes that are specific to them (such as numExamsToCreate in the Professor Object and numExamsToWrite in the Student Object.) The sub-classes do not have to repeat any of the code in the super-class. Fall 2018 CISC124 - Prof. McLeod
10
Some Goals of Hierarchy Design
All attributes in an instance (its “state”) should be defined with meaningful values. Minimize code repetition. The same attribute should not be declared in more than one class. A child class should be more concrete than its parent class. Design for polymorphism: Use abstraction (abstract classes and interfaces) where needed to ensure common behaviour and to allow for polymorphism. Fall 2018 CISC124 - Prof. McLeod
11
Some Goals of Hierarchy Design, Cont.
Control and minimize the public interface of all classes. Try not to have any “pass through” classes that do not contribute anything. Make sure the structure is extensible. Use a consistent variable and method naming scheme throughout the hierarchy. Follow the “real world” hierarchy as closely as possible. Fall 2018 CISC124 - Prof. McLeod
12
Back to the Example In Java, every new object you define is automatically a child of the Object class. So our actual structure is: Object Person Professor Student Fall 2018 CISC124 - Prof. McLeod
13
Example, Cont. This is a simplified UML Class Diagram. The upward pointing arrow shows the “is a” relationship. So: Person is a Object Professor is a Person Student is a Person Professor is a Object Student is a Object Fall 2018 CISC124 - Prof. McLeod
14
Aside - the Object Class
Object is the “Universal” parent class for every object in Java – in your code, in the API, everything! So, we are inheriting all the public members of this class – whether we want this stuff or not. What members are we getting and how do we find out? Fall 2018 CISC124 - Prof. McLeod
15
extends Keyword Creating the Person class: Which is the same as:
public class Person {…} Which is the same as: public class Person extends Object {…} But the “extends Object” is always there, so you don’t have to write it. Creating the Professor and Student classes: public class Professor extends Person {…} public class Student extends Person {…} Fall 2018 CISC124 - Prof. McLeod
16
Example, Cont. Suppose we wish to store the description of every person in this class (me included!) in one array. What array declaration would you use? The following example code is greatly simplified. It does not have any error checking, for example. Fall 2018 CISC124 - Prof. McLeod
17
Example, Cont. public class Person { private int age;
private String name; private String gender; public Person (int a, String n, String g) { age = a; name = n; gender = g; } // end Person constructor } // end Person Fall 2018 CISC124 - Prof. McLeod
18
Example, Cont. public class Professor extends Person {
private int numExamsToCreate; public Professor (int a, String n, String g, int e){ super(a, n, g); numExamsToCreate = e; } // end Professor constructor } // end Professor class Fall 2018 CISC124 - Prof. McLeod
19
Example, Cont. public class Student extends Person {
private int numExamsToWrite; public Student (int a, String n, String g, int e) { super(a, n, g); numExamsToWrite = e; } // end Student constructor } // end Student class Fall 2018 CISC124 - Prof. McLeod
20
super Keyword Provides a reference to the immediate parent class.
In the examples above, it was used to call the constructor in the parent class, Person. Note that the compiler will force you to invoke super in a child class’ constructor, and it must be the first line in the constructor. Fall 2018 CISC124 - Prof. McLeod
21
Example, Cont. public class ClassroomDB {
public static void main (String[] args) { Person[] db = new Person[250]; db[0] = new Student(18, "Fred", "male", 7); db[1] = new Student(19, "Melissa", "female", 7); db[2] = new Professor(29, "Alan", "male", 1); } // end main } // end ClassroomDB Fall 2018 CISC124 - Prof. McLeod
22
Polymorphism, Again Pointers of type Person are pointing to objects of type Student and Professor at runtime. At runtime the pointers will morph into their actual object types – this is late binding or dynamic binding. Fall 2018 CISC124 - Prof. McLeod
23
Methods in Sub-Classes
You have five ways to have methods in a sub-class: Write a new method (concrete or abstract). Inherit the method – it is available to use inside the sub-class or it can be invoked from an instance of the sub-class. Override the method. You must declare the method with exactly the same method declaration line, the same signature, as was used in the super-class. Overload the method in the super-class. Refine the method, by invoking the super class’s method in an overridden method. Fall 2018 CISC124 - Prof. McLeod
24
Inheriting Methods As we have discussed before, all public methods from the parent class are inherited by the child. And, of course, the parent class may have inherited a bunch of methods from its parent class – so every public method in the hierarchy above the current class is available. They behave just as if they were written in the child class. Fall 2018 CISC124 - Prof. McLeod
25
Inheriting Methods, Cont.
Note that an abstract sub-class can simply inherit an abstract method from an abstract super-class or an interface – it does not have to implement the method. In this case the sub-class will have to be abstract, as well. Suppose you don’t want all these methods – how can you avoid inheriting some, but not others? Fall 2018 CISC124 - Prof. McLeod
26
Method Overriding, Example
The Halloween5 object overrode the equals methods of the Object class: public boolean equals (Object obj) Fall 2018 CISC124 - Prof. McLeod
27
Method Overriding & Refining
When a sub-class overrides a method of the super-class, then it is the sub-class version that is invoked when called from an instance of the sub-class. Inside the sub-class, if you wish to invoke the super-class version of the overridden method, use the super keyword. If the overriding sub-class method invokes the super-class version of the same method, then it has refined the super-class method, as well as overriding it. Fall 2018 CISC124 - Prof. McLeod
28
Aside - the Use of the final Modifier
If “final” is included in a class header line, as in: public final class ClassName {…} Then the class cannot be extended. If you add “final” to a method definition, as in: public final void methodName () {…} Then the method cannot be overridden. Fall 2018 CISC124 - Prof. McLeod
29
Aside - the Effect of “private”
private methods and attributes are not inherited by the sub-class. But, you do inherit public methods and attributes. You can invoke public methods inherited from the parent class even when they themselves refer to attributes and methods that are private in the parent class. Fall 2018 CISC124 - Prof. McLeod
30
Overloading Methods If you use the same method name and return type as in the parent class, but a different parameter list in the sub-class, then you are just overloading the method. Fall 2018 CISC124 - Prof. McLeod
31
Aside - Multiple Inheritance?
This is when a sub-class extends more than one super-class. In our diagrams, a sub-class would point to more than one super-class. Java does not support multiple inheritance. (C++ allows it.) The designers of Java felt that multiple inheritance would lead to structures that are way too complex. Java designers have supplied the use of interfaces as a way of getting around this restriction. Fall 2018 CISC124 - Prof. McLeod
32
Aside - Multiple Inheritance?, Cont.
A sneaky way to get around the lack of multiple inheritance in Java is to combine two classes into one, by making one of them an Inner Class, and then you extend the outer class... Fall 2018 CISC124 - Prof. McLeod
33
Fall 2018 CISC124 Code Example Before going further, let us look at expanded code for the simple Person/Student/Professor inheritance structure. Some features to look for: Use of ArrayList<T> Overridden methods Refined methods Use of instanceof keyword Use of super keyword Use of getClass() Use of abstract keyword in an abstract class Polymorphism!! Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
34
Polymorphism, Cont. Where one type can appear as itself, but ends up being used as another type. Java is a very strongly typed language! Even so, you can allow one object to appear to be something else. In Java polymorphism must be constructed through object extension or interface implementation. Fall 2018 CISC124 - Prof. McLeod
35
Polymorphism - Late and Early Binding
Your program must always satisfy early binding for it to compile. Late binding occurs when the program is running, and only occurs when a variable of a parent-type object is pointing to a child object. The compiler does not (and cannot) check the late binding to see if it works. A failure of late binding results in a runtime error, not a compiler error. Fall 2018 CISC124 - Prof. McLeod
36
Aside – Where to Check Arguments?
Suppose the legal range of an argument depends on the type of the concrete object. But this attribute is declared in a base class, not in any of the concrete child classes. Where do you put the code to check the legality of this attribute and who will throw an exception? What is the order of operations? Is the attribute in the base class assigned before or after it is checked? Does this matter? Fall 2018 CISC124 - Prof. McLeod
37
Visualization of Hierarchies
The easiest way is to view the structure is as a UML (Unified Modeling Language) Class Diagram. This is what we have been doing, but normally more detail is shown. See our Person Hierarchy in a proper UML Class Diagram on the next slide: Fall 2018 CISC124 - Prof. McLeod
38
Fall 2018 CISC124 - Prof. McLeod
39
ArgoUML Eclipse can be configured to generate UML diagrams...
It is better to use a stand-alone tool, such as ArgoUML, available from: You can generate a diagram from existing code or even generate skeleton code from a diagram. Also, see “List of UML tools” in Wikipedia. Fall 2018 CISC124 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.