(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.

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

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
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.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
 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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Science I Inheritance Professor Evan Korth New York University.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
LECTURE 07 Programming using C# Inheritance
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
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.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
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.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Overview of C++ Polymorphism
Inheritance and Polymorphism
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)
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Object Oriented Programming
Inheritance.
Overview of C++ Polymorphism
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10

(c) University of Washington03-2 Composition: "has a" Classes and objects can be related in several ways One way: composition, aggregation, or reference Dog has-a owner, dog has legs, dog has collar, etc. In java: one object refers to another object via an instance variable public class Dog { private String name;// this dog's name private int age;//this dog's age private Person owner;// this dog's owner private Dog mother, father;// this dog’s parents private Color coatColor; //etc, etc. } One can think of the dog as "composed" of various objects: "composition“

(c) University of Washington03-3 Picturing the Relationships Dog Fido; //might be 6 years old, brown, owned by Marge, etc. Dog Apollo; //might be 2 years old, missing a leg, etc. In Java, it is a mistake to think of the parts of an object as being "inside" the whole. name legs owner color Fido

(c) University of Washington03-4 Drawing Names and Objects Names and objects Very different things! In general, names are applied to objects Objects can refer to other objects using instance variable names Fido (a name) refers to another object of type Dog Dog age 6 mother Dog

(c) University of Washington03-5 Drawing Names and Objects A name might not refer to any object (e.g. if null) One object might have more than one name i.e., might be more than one reference to it An object might not have any name “anonymous” Fido refers to another object of type Dog MyDoggie Fifi anonymous object of type Dog Dog age 6 mother Dog null

(c) University of Washington03-6 Specialization – "is a" Specialization relations can form classification hierarchies cats and dogs are special kinds of mammals; mammals and birds are special kinds of animals; animals and plants are special kinds of living things lines and triangles are special kinds of polygons; rectangles, ovals, and polygons are special kinds of shapes Keep in mind: Specialization is not the same as composition A cat "is-an" animal vs. a cat "has-a" tail

(c) University of Washington03-7 "is-a" in Programming Classes &/or interfaces can be related via specialization one class/interface is a special kind of another class/interface Rectangle class is a kind of Shape So far, we have seen one Java technique to capture this idea: interfaces Java interfaces are one special case of a more general design approach: Inheritance

(c) University of Washington03-8 Inheritance Java provides direct support for “is-a” relations likewise C++, C#, and other object-oriented languages Class inheritance one class can inherit from another class, meaning that it's is a special kind of the other Terminology Original class is called the base class or superclass Specializing class is called the derived class or subclass

(c) University of Washington03-9 Inheritance: The Main Programming Facts Subclass inherits all instance variables and methods of the inherited class All instance variables and methods of the superclass are automatically part of the subclass Constructors are a special case (later) Subclass can add additional methods and instance variables Subclass can provide different versions of inherited methods

(c) University of Washington03-10 A B A's stuff B's stuff A's stuff B's stuff B extends A A's stuff is automatically part of B

(c) University of Washington03-11 Interfaces vs. Class Inheritance An interface is a simple form of inheritance If B implements interface A, then B inherits the stuff in A (which is nothing but the method signatures of B) If B extends class A, then B inherits the stuff in A (which can include method code and instance variables) To distinguish the two, people sometimes say “interface inheritance” vs. “class inheritance”. What if you heard the phrase “code inheritance”?

(c) University of Washington03-12 Example: Representing Animals Generic Animal public class Animal { private int numLegs ; /** Return the number of legs */ public int getNumLegs ( ) { return this.numLegs; } /** Return the noise this animal makes */ public String noise ( ) { return "?"; } }

(c) University of Washington03-13 Specific Animals Cats public class Cat extends Animal { // inherit numLegs and getNumLegs() // additional inst. vars and methods …. /** Return the noise a cat makes */ public String noise( ) { return “meow"; } Dogs public class Dog extends Animal { // inherit numLegs and getNumLegs() // additional inst. vars and methods …. /** Return the noise a dog makes */ public String noise( ) { return “WOOF!!"; }

(c) University of Washington03-14 Animal Animal's stuff Cat Animal's stuff Cat's stuff Dog Animals's stuff Dog's stuff Cat extends Animal / Dog extends Animal

(c) University of Washington03-15 More Java If class D extends B /inherits from B... Class D inherits all methods and fields from class B But... "all" is too strong constructors are not inherited same is true of static methods and static fields although these static members are still available in the subclass Class D may contain additional (new) methods and fields But has no way to delete any methods or any fields of the B class (though D can override methods from B (very common) and hide fields (not recommended))

(c) University of Washington03-16 Never to be Forgotten If class D extends/inherits from B... a D can do anything that a B can do (because of inheritance) a D can be used in any context where a B is appropriate Every object of type D is also an object of type B

(c) University of Washington03-17 Method Overriding If class D extends B, class D may provide an alternative, replacement implementation of any method it would otherwise inherit from B The definition in D is said to override the definition in B An overriding method cannot change the number of arguments or their types, or the type of the result [why?] can only provide a different body Can you override an instance variable? Not exactly...

(c) University of Washington03-18 Polymorphism Polymorphic : "having many forms" A variable that can refer to objects of different types is said to be polymorphic Methods with polymorphic arguments are also said to be polymorphic public void speak(Animal a) { System.out.println(a.noise( )); } Polymorphic methods can be reused for many types

(c) University of Washington03-19 Static and Dynamic Types With polymorphism, we can distinguish between Static type: the declared type of the variable (fixed during execution) Dynamic type: the run-time class of the object the variable currently refers to (can change as program executes) public String noise() { // this has static type Animal... } Cat foofoo = new Cat( ); foofoo.noise (); //inside noise(), this has dynamic type Cat Dog fido = new Dog( ); fido.noise (); // inside noise(), this has dynamic type Dog

(c) University of Washington03-20 Dynamic Dispatch "Dispatch" refers to the act of actually placing a method in execution at run-time When types are static, the compiler knows exactly what method must execute. When types are dynamic... the compiler knows the name of the method – but there could be ambiguity about which version of the method will actually be needed at run-time. In this case, the decision is deferred until run-time, and we refer to it as dynamic dispatch

(c) University of Washington03-21 Method Lookup: How Dynamic Dispatch Works When a message is sent to an object, the right method to invoke is the one in the most specific class that the object is an instance of Makes sure that method overriding always has an effect Method lookup (a.k.a. dynamic dispatch ) algorithm: Start with the run-time class of the receiver object (not the static type!) Search that class for a matching method If one is found, invoke it Otherwise, go to the superclass, and continue searching Example: Animal a = new Cat( ); System.out.println(a.noise( )); a = new Dog( ); System.out.println(a.getNumLegs( ));

(c) University of Washington03-22 Summary Object-oriented programming is hugely important Lots of new concepts and terms Lots of new programming and modeling power Used more and more widely Ideas (so far!) Composition ("has a") vs. specialization ("is a") Inheritance Method overriding Polymorphism, static vs. dynamic types Method lookup, dynamic dispatch