CSE 1020:Inheritance Mark Shtern.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
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.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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)
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
CSC 205 Java Programming II
Chapter 11: Inheritance and Polymorphism
Inheritance in Java.
An Introduction to Inheritance
Object-Oriented Programming: Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Inheritance Basics Programming with Inheritance
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
CSE 1020:Programming by Delegation
CSE 1030: Data Structure Mark Shtern.
Computer Programming with JAVA
Polymorphism and Type Casting
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Inheritance and Polymorphism
Presentation transcript:

CSE 1020:Inheritance Mark Shtern

Course evaluations Lecture: Tuesday, July 12 Lab: Tuesday, July 12 Both will take place at the beginning of the lecture / lab

Summary Aggregation Collection Composition Traversing

UML

Definition C extends P: Every fields and public method present in P is also present in C Extend is inheritance relation between the child and parent C is a P C is specialization of P and P is generalization of C

UML diagram

Inheritance Inheritance chain Inheritance hierarchy Descendant Java does not allow multiple- inheritance

Subclass API The constructor section The method section Overriding methods Override vs Overload The field section Shadow Field

Example 903 Examine API of the reward card subclass and its supper class, and identify the methods that were Overridden Added by subclass Provide a rational to justify why these methods were overridden or added

The substitutability Principle It says: When parent is expected, a child is accepted output.println(“Enter C or P”); char choice = input.nextChar(); P x; If (choice ==‘P’) { x = new P(); } else { x = new C(); }

Example 904 Write a fragment that asks the user whether an ordinary or a reward card is wanted, then create the appropriate card with card number 9 and cardholder name “Adam” CreditCard card; If (type.equals(“O”)) { card = new CreditCard(9,”Adam”); } else { card = new RewardCard(9,”Adam”); }

Early and Late Binding The compiler performs early binding as before Assuming no errors, it culminates in a target VM performs late binding Determine the class of the object If reference null, then a “Null Point Exception” is thrown Searches for method with matching signature and binds with it

What is expected output CreditCard card1 = new RewardCard(9, “Adam”); CreditCard card2 = new RewardCard(9, “Adam”); card1.charge(500); card1.pay(500); output.println(card1.isSimilar(card2));

Polymorphism CreditCard card; .... Card.charge(500.0); Polymorphic is capable of having multiple forms Polymorphism refers to the ability of the same entity to have multiple forms instanceof

Predict output of the following code CreditCard card1 = new RewardCard(9, “Adam”); CreditCard card2 = new RewardCard(9, “Adam”); card1.charge(500); card1.pay(100); output.println(card1.isSimilar(card2)); output.println(card1.isSimilar((RewardCard)card2)); output.println((RewardCard)card1.isSimilar(card2)); output.println((RewardCard)card1.isSimilar((RewardCard)card2));

Abstract Class Abstract class does not encapsulate an actual object

Abstract Class API public abstract class Vehicle Factory Method Vehicle myCar = Vehicle.createCar(); Subclass constructor Vehicle myCar = new Car();

Example 908 Create an instance of the type Calendar

Interface Interface retains only Constants Contracts of the shared methods

Interface A class contains implementation of all interface methods is said to implement interface Implementation is-a relation A class can implement as many interfaces as needed

Object Class Boolean equals(Object other) String toString() Class getClass()

Generic Generic or Parameterized Type The generic idea allows an implementer to write a component that can handle only one type T but without specifying T Bar<CreaditCard> bag = new Bag<CreaditCard>();

Summary Inheritance, Polymorphism and Substitutability, Binding Interfaces, Abstract class Generic

Exercise 9.4 (modified) Determine the API of class Bee in this UML diagram

Exercise 905678 Based on the inheritance chain determine whether the following fragments have Compiler-error Run-time error Justify the cast in

Exercise 920 The following fragment seeks to invoke toString method of the Object class in order to determine the memory address of a Fraction instance Object tmp = new Fraction(); output.println(tmp.toString()); output.println((Object)tmp.toString()); Predict and Explain the output

Exercise 922 Consider the following fragment Fraction x = new Money(2.25); Money y = new Money(5.27); output.println(x.resembles(y)); output.println(((MixedNumber)x).resembles(y)); output.println(((Money)x).resembles(y)); Predict output

Same as previous (Exercise 921) Fraction x = new Money(2.25); Fraction xFr = new Fraction(225, 100); Fraction yFr = new Fraction(527, 100); output.println(xFr.resembles(yFr)); MixedNumber xMi = new MixedNumber(1, 2, 25, 100); MixedNumber yMi = new MixedNumber(1, 5, 27, 100); output.println(xMi.resembles(yMi)); Money xMo = new Money(2.25); Money yMo = new Money(5.27); output.println(xMo.resembles(yMo));

Exercise 9.16 Write a program that generates statistics about the return type of the getRandom method of the MixedNumber class Invoke 100 times and output the number of times a Fraction or a MixedNumber was returned Use instanceof

Exercise 9.17 Write a program that generates statistics about the return type of the getRandom method of the MixedNumber class Invoke 100 times and output the number of times a Fraction or a MixedNumber was returned Use getClass