Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Inheritance in Java.
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Chapter 9: Polymorphism and Inheritance
CISC124 Assignment 4 on Inheritance due today at 7pm.
Computer Programming with JAVA
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Fundaments of Game Design
CMPE212 – Reminders Assignment 3 due next Friday.
Review of Previous Lesson
Chapter 8 Class Inheritance and Interfaces
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Final and Abstract Classes
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Presentation transcript:

Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders Assignment 3 due this Friday. The assignment statement and the files linked to the assignment have been updated. Quiz 2 today, in the lab. See the slides from last Wednesday’s lecture for Quiz topics. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Winter 2019 CMPE212 4/5/2019 Today A quick look at some Collection<T> interfaces and abstract classes to see the mix of concrete, abstract, inherited and default methods. Start Inheritance. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Interface Implement Extend Abstract Class Concrete Class Collection<T> Extend Abstract Class 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 2019 CMPE212 - Prof. McLeod Stack<T>

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

Why Bother? One reason is code re-use, and to minimize code duplication. 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 can be part of the polymorphism mechanism. Many classes in the Java API are intended for use as parent classes. Winter 2019 CMPE212 - Prof. McLeod

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

https://en.wikipedia.org/wiki/List_of_popular_music_genres Music Hierarchy For example, see: https://en.wikipedia.org/wiki/List_of_popular_music_genres 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 2019 CMPE212 - Prof. McLeod

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

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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

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

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

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

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

Method Overriding, Example The Halloween5 object overrode the equals methods of the Object class: public boolean equals (Object obj) Winter 2019 CMPE212 - Prof. McLeod

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

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

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

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

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 too complex. Java designers have supplied the use of interfaces with multiple implementations as a way of getting around this restriction. Winter 2019 CMPE212 - Prof. McLeod

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

Winter 2019 CMPE212 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!! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod