CSC 143 Java Composition - Seattle Central College

CSC 143 Java Composition - Seattle Central College

Composition: "has a" • Classes and objects can be related in several ways • One way: composition, aggregation, or reference • Dog has-a owner, dog has...

NAN Sizes 0 Downloads 1 Views

Recommend Documents

SEATTLE CENTRAL COMMUNITY COLLEGE
Text: Elementary Algebra, A Modeling Approach by Katherine Yoshiwara (Not required). Aleks Student Access Code: Purchase

Eco201 - Seattle Central College
Canadian dollar is 529 forints per Big Mac divided by 3.27 Canadian dollars per Big Mac equals. 162 forints per Canadian

Logic Gates and Properties - Seattle Central College
We can now examine some basic properties of logical operations and how these ... To see that the Distributive Law holds,

Theory of Consumer Choice - Seattle Central College
Chap 21: Theory of Consumer Choice. • This chapter develops a theory that describes how consumers make decisions about

Test 1 Fall07 Key - Seattle Central College
1 ceiling would have on the market for gum-ball ' 5 machines. What is the quantity sold? Would this $800 . be a binding

credit card form - Seattle Central College
SEATTLE CENTRAL COLLEGE – DIVISION OF REGISTRATION AND RECORDS. 1701 Broadway, BE1104 Seattle, Washington 98122. Credi

Java 101 primer: Composition and inheritance
Oct 22, 2015 - Download the source code for “Java 101 primer: Composition ... recall from “Java 101: Classes and obj

seattle central library - 2030 Districts
The Library achieved LEED Silver certification and the overall energy performance already exceeds the 2030 goal. Owner:

Seattle Central Library.indd - STRUCTURE magazine
Seattle. Central. Library is a building of many stories. One of the most interesting is that ..... Seattle Public Librar

Java . . .
19. Answers. — Output of each println statement: a b c. \\. ' """ C: in he downward spiral. — println st

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

CSC 143 Java

• via an instance variable public class Dog { private String name; private int age; private Person owner; private Dog mother, father; private Color coatColor; }

Inheritance Reading: Ch. 10

// this dog's name //this dog's age // this dog's owner // this dog’s parents //etc, etc.

• One can think of the dog as "composed" of various objects: "composition“ (c) University of Washington

03-1

(c) University of Washington

Picturing the Relationships

Drawing Names and Objects

• 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. Fido legs

03-3

• 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

Dog age

6

another object of type Dog

Dog

(c) University of Washington

03-4

Specialization – "is a"

Drawing Names and Objects

• Specialization relations can form classification hierarchies anonymous object of type Dog

Dog

• “anonymous”

• 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

MyDoggie null Dog age refers to

• Objects can refer to other objects using instance variable names

mother

(c) University of Washington

Fifi

• Very different things!

• In general, names are applied to objects

refers to

owner

color

Fido

• Names and objects

Fido (a name)

name

03-2

6

Dog

another object of type Dog

• A cat "is-an" animal vs. a cat "has-a" tail

mother

(c) University of Washington

03-5

(c) University of Washington

03-6

03-1

"is-a" in Programming

Inheritance

• Classes &/or interfaces can be related via specialization

• Java provides direct support for “is- a” relations

• one class/interface is a special kind of another class/interface • Rectangle class is a kind of Shape

• likewise C++, C#, and other object-oriented languages

• Class inheritance

• 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 Washington

• 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

03-7

(c) University of Washington

Inheritance: The Main Programming Facts

03-8

B extends A

• Subclass inherits all instance variables and methods of the inherited class

A

• 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

B

A's stuff

A's stuff

A's stuff is automatically part of B

B's stuff

(c) University of Washington

03-9

(c) University of Washington

03-10

Example: Representing Animals

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”?

• 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 Washington

03-11

(c) University of Washington

03-12

03-2

Specific Animals • Cats

Cat extends Animal / Dog extends Animal

• Dogs

public class Cat extends Animal { // inherit numLegs and getNumLegs()

public class Dog extends Animal { // inherit numLegs and getNumLegs()

// additional inst. vars and methods ….

// additional inst. vars and methods ….

/** Return the noise a cat makes */ public String noise( ) { return “meow"; }

/** Return the noise a dog makes */ public String noise( ) { return “WOOF!!"; }

}

}

(c) University of Washington

Animal

Cat

Animal's stuff

Animal's stuff

Dog

Cat's stuff

03-13

More Java

Animals's stuff Dog's stuff

(c) University of Washington

03-14

Never to be Forgotten

If class D extends B /inherits from B... • Class D inherits all methods and fields from class B • But... "all" is too strong

If class D extends/inherits from B...

Every object of type D is also an object of type B

• 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 Washington

03-15

Method Overriding

• 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 (c) University of Washington

03-16

Polymorphism

• 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

• 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( )); }

• 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

• Polymorphic methods can be reused for many types

• Can you override an instance variable? • Not exactly... (c) University of Washington

03-17

(c) University of Washington

03-18

03-3

Static and Dynamic Types

Dynamic Dispatch

• 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

• In this case, the decision is deferred until run-time, and we refer to it as dynamic dispatch

Dog fido = new Dog( ); fido.noise (); // inside noise(), this has dynamic type Dog

(c) University of Washington

• "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.

03-19

(c) University of Washington

Summary

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

• 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

• Example: Animal a = new Cat( ); System.out.println(a.noise( )); a = new Dog( ); System.out.println(a.getNumLegs( )); (c) University of Washington

03-20

03-21

(c) University of Washington

03-22

03-4