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.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
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.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
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.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
An Introduction to Inheritance
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…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Object-Oriented Programming: Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 3 due next Friday.
Chapter 8 Class Inheritance and Interfaces
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.
Presentation transcript:

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 Encapsulation, JUnit Testing and Inheritance. Includes Nested Classes. Winter 2016CMPE212 - Prof. McLeod1

Today Inheritance, Cont.: –Methods in Sub-Classes. –Multiple Inheritance. –More detailed Person code example. –UML Tools for Visualization. –Another code example – the drawing hierarchy. Winter 2016CMPE212 - Prof. McLeod2

Winter 2016CMPE212 - Prof. McLeod3 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 2016CMPE212 - Prof. McLeod4 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 it’s 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 2016CMPE212 - Prof. McLeod5 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 2016CMPE212 - Prof. McLeod6 Method Overriding, Example The Halloween4 object overrode the equals methods of the Object class: public boolean equals (Object obj)

Winter 2016CMPE212 - Prof. McLeod7 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 2016CMPE212 - Prof. McLeod8 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 2016CMPE212 - Prof. McLeod9 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 2016CMPE212 - Prof. McLeod10 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 2016CMPE212 - Prof. McLeod11 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.

Winter 2016CMPE212 - Prof. McLeod12 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 2016CMPE212 - Prof. McLeod13 Code Example Let us look at expanded code for the simple Person/Student/Professor inheritance structure. Some features to look for: –Use of ArrayList –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 2016CMPE212 - Prof. McLeod14 Polymorphism, Second Pass 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.

Winter 2016CMPE212 - Prof. McLeod15 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.

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 Heirarchy in a proper UML Class Diagram on the next slide: Winter 2016CMPE212 - Prof. McLeod16

Winter 2016CMPE212 - Prof. McLeod17

ArgoUML Eclipse can be configured to generate UML diagrams... Or, you can 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. Winter 2016CMPE212 - Prof. McLeod18

Another Example: A Drawing Hierarchy Winter 2016CMPE212 - Prof. McLeod19 Fillable DrawItem DrawLineItem Image Rectangle Square FreeFormLine Interface Concrete Class Implement Extend Key: Abstract Class

Aside – Text Representation of Hierarchy From the generated javadoc for this example: Winter 2016CMPE212 - Prof. McLeod20

Another Example, Cont. Features to look for: –Inheritance –Abstract Classes –An Interface –Method overriding –Use of instanceof –Polymorphism Winter 2016CMPE212 - Prof. McLeod21

Another Example, Cont. This example is missing a great deal! No error checking, no actual drawing code, etc. No commenting. Note that no attribute is declared in more than one place. Minimal redundancy! Note how easy it is to use a collection of DrawItem objects. Can you think of another way of structuring this that would not require the interface? Winter 2016CMPE212 - Prof. McLeod22