Two big words Inheritance Polymorphism

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
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
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Enhanced-ER (EER) Model Concepts.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams 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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Enhanced Entity-Relationship (EER) Model
© Shamkant B. Navathe CC.
JAVA By Waqas.
Lecture 12 Inheritance.
Enhanced Entity-Relationship (EER) Modeling
Session 2 Welcome: The sixth learning sequence
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Inheritance in Java.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
CSC 205 Java Programming II
© Shamkant B. Navathe CC.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
© Shamkant B. Navathe CC.
Java Inheritance.
Fundaments of Game Design
Inheritance and Polymorphism
CSC 111 Exam 3 Review Fall 2005.
Review of Previous Lesson
© Shamkant B. Navathe CC.
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Enhanced Entity-Relationship (EER) Modeling
Foundations of Programming 2: Inheritance and Polymorphism
Computer Science II for Majors
Presentation transcript:

Two big words Inheritance Polymorphism Defining a class that, by default, has all the same behavior of some other (parent) class We can re-define some of the parent functions/attributes We can add new behaviors and attributes to supplement the original Polymorphism We can take advantage of the fact that all of a certain set of classes have common attributes/behaviors Can avoid redundancy by specifying requirements to be in the particular class

Subclasses Used to add member functions/attributes to existing classes, without modifying the original Syntax:

Subclasses Naturally, we can apply the same principle to subclasses, extending them further This creates a class-hierarchy

Subclasses In the left diagram: A is the superclass of B, C, D, and E All others have all members of A B, C, and D share existing attributes/behaviors of A, but may have totally independent features E inherits from both D and A

Subclasses Example: vehicle database What are the defining attributes of a vehicle? Color Make Model Number of wheels Variable, but can also be an indication of more fine-grained classification (see right)

Vehicles Idea: Can add more specific attributes: Define the vehicle class to have the attribute: int numberOfWheels; In the subclasses, we can specify exactly how many in the constructor or class definition Can add more specific attributes: Car: numberOfDoors Truck: numberOfAxels Motorcycle: hasSideCar

Class and Subclass Variables Very Important Fact An variable that can hold a reference to an object of class A can also hold a reference to an object of any subclass of A. E.g. if “Car” is a subclass of “Vehicle”, the following is legal: Vehicle v; Car c = new Car(); v = c; // We don’t even lose the Car information!

Polymorphism With some planning, we can use some objects/classes in our program without knowing exactly what they are at runtime (!!!) Recall that: Very Important Fact: any variable that holds a reference to a class can also hold a reference to any of its subclasses Subclasses can override base class members

Polymorphism Architectural idea Write functions to accept the most general type of parameter possible, assuming all subclasses have responsibly overridden any necessary methods We have seen this already in the form of the Object.toString() method override!

Polymorphism Example: Example 2: In our vehicle example: make sure each vehicle defines its own “print()” method Add a method in the driver class to call “.print()” on a Vehicle parameter Example 2: Add an “upgrade()” method to the Vehicle class Do nothing Override in each subclass, upgrading where appropriate Upgrade some vehicles, then print