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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
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.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
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.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Chapter 9: Polymorphism and Inheritance
Advanced Java Programming
CISC124 Assignment 4 on Inheritance due today at 7pm.
Computer Programming with JAVA
CISC124 Assignment 4 on Inheritance due next Friday.
Inheritance Inheritance is a fundamental Object Oriented concept
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Java Inheritance.
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
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Chapter 14 Abstract Classes and Interfaces
CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
Review of Previous Lesson
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Final and Abstract Classes
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

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

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

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

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

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

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

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

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

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>

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

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

Real Life Hierarchies Can you think of any? Flora, Fauna Rocks & Minerals Food Menus Fasteners Vehicles Computers etc! Winter 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 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 2018 CMPE212 - Prof. McLeod

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