polymorphism Inheritance Base class Derived class

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
More about classes and objects Classes in Visual Basic.NET.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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,
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Week Inheritance and Polymorphism. Introduction Classes allow you to modify a program without really making changes to it. To elaborate, by subclassing.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Inheritance New class derived from existing class Software engineering technique Software reuse : faster, easier, cheaper Parent class (supper class):
Advanced Programming in Java
Guide to Programming with Python
Polymorphism.
Inheritance Inheritance allows a software developer to derive a new class by extending an existing one. The existing class is called the parent class or.
Interface, Subclass, and Abstract Class Review
One class is an extension of another.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Computing with C# and the .NET Framework
Object Oriented Programming in Python
Week 8 Lecture -3 Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
One class is an extension of another.
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Code reuse through subtyping
Chapter 11 Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Inheritance CT1513.
Chapter 9 Carrano Chapter 10 Small Java
What is the "animal " doing ?.
CSE 231 Lab 13.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Programming in C# CHAPTER 5 & 6
Presentation transcript:

polymorphism Inheritance Base class Derived class

recall from last class -polymorphism r = a rectangle t = a triangle c = a circle txt = some text shapeList=[r,t,c,txt] for shape in shapeList: shape.draw(win)

Inheritance mammal, horse, kangaroo mammal horse kangaroo

Inheritance mammal, horse, kangaroo mammal backbone fur mammal horse 4 legs tail kangaroo backbone fur pocket tail horse kangaroo

Inheritance Inheritance :derive a new class from an existing one existing class:the parent class, or superclass, or base class The new class: derived class ,the child class or subclass the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class Copyright © 2012 Pearson Education, Inc.

is-a relationship get all the member variables get all the member methods if you want. can add more variables can add or modify methods software reuse is good – already tested, etc.

Base class: Animal class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")

Base class: Animal - constructor class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies Has instance data self.name (a string) self.flies(boolean, default is False)

Base class: Animal – other methods def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, # unchanged, for Duck. def eat(self): print("Eat, eat, eat.")

Derived class: Duck class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers

Derived class: Duck - constructor class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers instance data self.name (from Animal) self.flies (from Animal) self.feathers (just for Duck)

Derived class: Duck – other methods #This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk totally overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")

main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move()

class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")

main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() just an animal self.name False self.flies a just an animal Indistinct animal noises Eat, eat, eat.

class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")

class Duck(Animal): #The derived constructor calls the base constructor, #then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers #This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk total overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")

main just an animal Indistinct animal noises Eat, eat, eat. I am a duck with 1000 feathers Quack! Quack! I swim. I fly. I walk def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() a just an animal self.name False self.flies 1000 self.feathers duck self.name False True self.flies d