Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…

Similar presentations


Presentation on theme: "Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…"— Presentation transcript:

1 Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff… Quiz 2 in the lab next week. More info in last lecture’s slides. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Today Finish up Interfaces. How to view Java API source code.
Start Hierarchies or “Inheritance” (not on Quiz 2). Winter 2018 CMPE212 - Prof. McLeod

3 Another Example on Interfaces
Look at DoRunRunDemo and see how it works. The example contains examples of how the interface can (and cannot) be used in Java 8 and 9. Winter 2018 CMPE212 - Prof. McLeod

4 Summary of Interface Demo
Inside the interface: Can only have public final static attributes. Can have public abstract, public static, public default, private and private static methods. Do not use the public or abstract access modifiers. Public static methods can invoke private static methods. Public default methods can invoke private non-static methods. Winter 2018 CMPE212 - Prof. McLeod

5 Summary of Interface Demo, Cont.
Inside a class implementing the interface: Must implement all abstract methods (or this class will be abstract). Constants are inherited. Default methods can be inherited, made abstract (in which case the class will be abstract) or overridden. Public static methods can be invoked from the interface directly. Winter 2018 CMPE212 - Prof. McLeod

6 Summary of Interface Demo, Cont.
Inside some other class: Constants and methods can be accessed directly through the interface. The interface cannot be instantiated. A class implementing the interface can be instantiated. Pointers can be declared to be of the interface type and they can then point to instances of a class that implements the interface. A class implementing the interface can be cast to be of the interface type. Winter 2018 CMPE212 - Prof. McLeod

7 Aside – Viewing Java API Source Code
Open up the library link. Most of what we are interested in will be in rt.jar. When you locate the class or interface of interest and are doing this for the first time, you will see: Winter 2018 CMPE212 - Prof. McLeod

8 Aside – Viewing Java API Source Code, Cont.
Click on “Attach Source…” Fill out the dialog by providing the location of the src.zip file for the JDK you are using: Winter 2018 CMPE212 - Prof. McLeod

9 Hierarchy Example from the Java API
Winter 2018 CMPE212 Hierarchy Example from the Java API A quick look at part of an existing hierarchy – the Collection<T> hierarchy – it won’t hurt and it shows the use of Interfaces and Abstract classes: Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

10 Interface Implement Abstract Class Extend Concrete Class
Collection<T> Abstract Class Extend Concrete Class Set<T> List<T> Queue<T> AbstractCollection<T> SortedSet<T> AbstractQueue<T> AbstractSet<T> AbstractList<T> PriorityQueue<T> EnumSet<T> TreeSet<T> AbstractSequentialList<T> ArrayList<T> HashSet<T> Vector<T> LinkedList<T> LinkedHashSet<T> Winter 2018 CMPE212 - Prof. McLeod Stack<T>

11 Winter 2018 CMPE212 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. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

12 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. Winter 2018 CMPE212 - Prof. McLeod

13 Real Life Hierarchies Can you think of any? Flora, Fauna
Rocks & Minerals Food Menus Fasteners Vehicles Computers etc! Winter 2018 CMPE212 - Prof. McLeod

14 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. Winter 2018 CMPE212 - Prof. McLeod

15 Example Our classroom: Person Professor Student
Both Professor and Student are sub-classes (or children) of the super-class or (parent) Person. Winter 2018 CMPE212 - Prof. McLeod

16 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. Winter 2018 CMPE212 - Prof. McLeod

17 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. Winter 2018 CMPE212 - Prof. McLeod

18 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) to ensure common behaviour and to allow for polymorphism. Winter 2018 CMPE212 - Prof. McLeod

19 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. Winter 2018 CMPE212 - Prof. McLeod

20 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 Winter 2018 CMPE212 - Prof. McLeod

21 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 Winter 2018 CMPE212 - Prof. McLeod

22 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? Winter 2018 CMPE212 - Prof. McLeod

23 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 {…} Winter 2018 CMPE212 - Prof. McLeod

24 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. Winter 2018 CMPE212 - Prof. McLeod

25 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 Winter 2018 CMPE212 - Prof. McLeod

26 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 Winter 2018 CMPE212 - Prof. McLeod

27 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 Winter 2018 CMPE212 - Prof. McLeod

28 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. Winter 2018 CMPE212 - Prof. McLeod

29 Example, Cont. public class ClassroomDB {
public static void main (String[] args) { Person[] db = new Person[50]; 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 Winter 2018 CMPE212 - Prof. McLeod


Download ppt "Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…"

Similar presentations


Ads by Google