The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Object Oriented Programming
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Intro to OOP with Java, C. Thomas Wu
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Inheritance in Java.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Advanced Programming in Java
Computing with C# and the .NET Framework
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
CSC 205 Java Programming II
Modeling with Inheritance
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

The child gets it all.

 Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics  promotes code re-use, avoids duplication & errors  Specialization  child class redefines behavior of the parent  Extension  child class adds new attributes or behavior

 Provide common behavior  implement once in the superclass, all children inherit it  Example: all bank accounts have account ID, owner, balance BankAccount # accountName # accountID # balance + deposit( ) : void + withdraw( ) : void + getBalance( ) : double + getAcctName( ) : String...etc... CheckingAccount + CheckingAccount( ) + withdraw( ) : void + toString( ) : String SavingAccount + SavingAccount( ) + withdraw( ) : void + toString( ) : String

 An object of the subclass can be used anywhere that an object of the superclass is expected In a program, if all objects of the superclass are replaced by objects from a subclass, the program should still work correctly. OR  Subtypes must be substitutable for their base types

 A subclass can override (redefine) a method inherited from the parent, in order to specialize the behavior.  The subclass specializes the behavior for its own needs public class Person { protected string name; public string ToString() { return name; } } public class Student : Person { protected string studentID; // redefine ToString() to return our ID, too. public string ToString() { return string.Format(“{0} {1} “, name, studentID); }

Bank accounts have different rules for deposit and withdraw, so we specialize (redefine) these methods in SavingAccount and CheckingAccount BankAccount + deposit( ) : void + withdraw( ) : void + getBalance( ) : double + getAcctName( ) : String...etc... CheckingAccount + deposit( ) : void + withdraw( ) : void SavingAccount + deposit( ) : void + withdraw( ) : void

 A subclass cannot "reduce visibility" of a method it redefines from parent.  Example: Object ToString() is public. A subclass cannot define its own ToString() to be protected or private.  assign weaker access permissions is OK. Visibility in Parent ClassVisibility in Child Classpublic ToString( ) protected setName( )protected or public OK private getRadix( ): int anything -- private method is statically bound, visible only inside parent class.

Why these access rules?  Its a consequence of the Substitution Principle "An object of a child class can be substituted any where that an object of the parent class is expected"  Example: the Greeter class expects a Person object and issues a greeting: public class Greeter { public static void greet( Person p ) { Console.WriteLine("Hello " + p.ToString() ) } Person joe = new Person("Joe Nerd"); Greeter.greet( joe ); joe = new Student("Joe Scholar", " "); Greeter.greet( joe ); Student ToString( ) must be at least as visible as Person ToString( )

public class Person { protected string name; protected long ID; public long getID() { return ID; } } public class Student : Person { private string ID;// OK to redefine ID! // OK to redefine method: visibility & type same public string ToString() { return name+" "+ID; } // Illegal! (1) less visible, (2) change type protected string getID() { return ID; } }

 Parent's data members and methods are not replaced by child's members, they are simply shadowed.  Use “ base " to access parent's members (except private ones). (in java the keyword for this is “super”) public class Person { protected string name; protected long ID; public string ToString() { return name; } } public class Student : Person { private string ID;// OK to redefine ID! public string ToString() { return name + " " + base.ID; // parent's ID }

 A subclass can define new behavior that the superclass does not have.  A subclass can also define new attributes. public class Person { protected string name; public string ToString() { return name; } } public class Student : Person { protected int credits; // new attribute // new behavior public void addToCredits(int n) { credits += n; } public void getCredits( ) { return credits; } }

A simple test for whether inheritance is reasonable is the "is a" rule.  a Student is a Person  Student extends Person  a Button is a WebControl  Button extends System.Web.UI.WebControls

 Apply the “is a” rule

 In the case of a Tub, we would say: "a Bathroom has a Tub"  "has a" means that something should be an attribute  "has a" indicates an association.  UML uses an open arrowhead for association Bathroom - items : Tub Tub 1

The "is a" rule does not always indicate inheritance.  Barak Obama is a Person  BarakObama is an instance of the Person class  A C # book is a Book  Java book doesn't specialize any behavior of Book. This is a classifier that can be modeled using an attribute.  A Fraction is Comparable  Describes a kind of behavior that Fractions possess. Model this using an interface.

 Subclass doesn't add significant extension or specialization Example: a library has several types of recordings: Jazz Recording, Classical Recording, Pop Recording,... Recordings may be Tape or CDROM... Recording JazzRecordingClassicalRecordingPopRecording JazzTapeRecordingJazzCDROMRecording

 Don't use a subclass in situations where an object may need to change class during its life time. Example: Full-time and Part-time students have different requirements and behavior. Should we model this using inheritance? Student PartTimeStudentFullTimeStudent

 A student could change from Full-time to Part-time... and back again!  Full-time or part-time is a role or status.  A Part-time student may have some behavior that is different from Full-time student. Model this using an attribute of Student that references an object of the appropriate status. attendance PartTimeFullTime Student Enrollment 1 *

 C++ has multiple inheritance: subclasses can "mix in" the properties of several parent classes. class iostream: public istream, public ostream { public: iostream( streambuf* ); virtual ~iostream(); protected: iostream( ); } istream ostream iostream

 Java and C# do not allow multiple inheritance.  In many situations interfaces can substitute for use of multiple inheritance. public class Student extends Person, Thread {... } Error: no multiple inheritance public class Student extends Person implements IRunnable {... } Runnable interface for Threaded application

 Java and C# do not allow multiple inheritance.  In many situations interfaces can substitute for use of multiple inheritance. public class Student : Person, Thread {... } Error: no multiple inheritance public class Student : Person, IRunnable {... } Runnable interface for Threaded application

 In our game we have 2 objects that are very similar  If we take what is similar and create a base object, then we eliminate duplication of code.

GAME OBJECTTARGET OBJECT

 Right click on the library(engine) project > Add > New Item  Pick “Class”

TARGET OBJECTBASE OBJECT

 What happens if you don’t?

 Compile  Remove the reference from the game project  Add the reference back in (just to make sure the changes are in the game)