Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Object Oriented Software Development
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
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.
BY:- TOPS Technologies
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Polymorphism.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Inheritance and Encapsulation
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Designing for Inheritance
Chapter 9 Inheritance and Polymorphism
Lecture 19 - Inheritance (Contd).
More about inheritance
Polymorphism CT1513.
Polymorphism, Abstract Classes & Interfaces
Presentation transcript:

Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming

Reducing User Complexity Inheritance makes development easier May make things more complex for user –Need to construct, store, manipulate many different types of objects Employee HourlySalaried Shareholder

Reducing User Complexity Goal of polymorphism: Allow user to treat all objects as instances of the superclass Employee HourlySalaried Shareholder User just needs to know how to use superclass Subclasses can be treated like part of internal representation

Polymorphism Can assign subclass object to superclass variable Clock c = new SecondClock (12,34,56); Can make changes dynamically Clock c; c = new Clock(14,34); c = new SecondClock(12,34,56);

Polymorphisn How is this possible? Subclass object has all methods of superclass –Inherited –Overridden Clock c = new SecondClock(12,34,56); c.setHour(8); c.nextMinute(); s = c.toString(); … All of these methods also defined for SecondClock objects!

Polymorphism Note: can only use superclass methods Clock c = new SecondClock(12,34,56); c.setHour(9); c.setMinute(21); c.setSecond(30); Methods used in polymorphism must be defined in the superclass Legal, since setHour and setMinute defined for both Clock and SecondClock Illegal, since setSecond only defined for SecondClock

Run-time Evaluation JVM chooses version of overridden method based on actual object type Example: overridden toString() method Clock c; c = new Clock(14,34); System.out.println(c.toString()); c = new SecondClock(12,34,56); System.out.println(c.toString()); Uses Clock ’s toString() “ 14:34 ” Uses SecondClock ’s toString() “ 12:34:56 ”

Run-time Evaluation Note that this decision must be made at run time based on current type of object Clock c; if (Math.random() > 0.5) c = new Clock(14,34); else c = new SecondClock(12,34,56); System.out.println(c.toString()); Tradeoff of representational power vs. run speed –C++ requires such methods be explicitly declared “virtual” –By default, makes decision about object type at compile time Will not know which version to use until run this line of code

Uses of Polymorphism Storing different types in single data structure –Define array of superclass type –Store subclass objects in it Employee workers = new Employee[100]; workers[0] = new Hourly(“Fred”, 10, 40); workers[1] = new Salaried(“Barney”, 26000); workers[2] = new Shareholder(“Slade”, 52000, 1000); …

Uses of Polymorphism Defining single method that takes multiple types of parameter –Use superclass type in method definition –Pass subclass objects when called JPanel panel = new JPanel(); p.add(infoLabel); // add JLabel p.add(nameField); // add JTextField p.add(startButton); // add JButton …

Uses of Polymorphism add method in JPanel takes JComponent as parameter Superclass of all visual components –Defines x, y, width, and height of component –All the JPanel needs to display on surface public class JPanel { … public void add(JComponent c) { … … JPanel p = new JPanel(); p.add(infoLabel); Assigning subclass object to superclass variable