Download presentation
Presentation is loading. Please wait.
Published byCory Barrett Modified over 9 years ago
1
Question of the Day Thieves guild states it will sell to members: lock picking kits $0.67 each 40’ rope $2.12 each Wire cutters $4.49 each How much would one pay for 4 lock picking kits, 8 ropes, and 5 wire cutters?
2
Question of the Day Thieves guild Thieves guild states it will sell to members: lock picking kits $0.67 each 40’ rope $2.12 each Wire cutters $4.49 each How much would one pay for 4 lock picking kits, 8 ropes, and 5 wire cutters? Nothing, they’d steal it.
4
Sharing Among Classes
5
Composition has-a Used when there is “has-a” relationship has a Student has a name has a Full name has a first name has an Car has an engine has an Rectangle has an upper right vertex Use a field to compose classes So we would add name field to Student class firstName field in FullName class If must use data externally, add getters & setters
6
Inheritance Is-a “Is-a” relationships implemented via inheritance is a Automobile is a Vehicle is a Car is a Vehicle is an Truck is an Automobile is an Car is an Automobile extends Subclass extends superclass for inheritance extends Automobile extends Vehicle extends Truck extends Automobile extends Car extends Automobile
7
Example of an Object Hierarchy extends extends extends public class Vehicle {…} public class Automobile extends Vehicle {…} public class Car extends Automobile {…} public class Truck extends Automobile {…} ObjectVehicleAutomobileCarTruck
8
What Gets Inherited And How? Class inherits all members from superclass Subclass can use them directly unless they are private Use as if they were copied into class w/o copying
9
What Gets Inherited And How?
12
What About Constructors? "is-a" Fields and methods inherited since have "is-a" Car is NOT a Vehicle without same data & actions Adding members allowed, since Car more specific But we (almost) always have access to fields & methods Only during instantiation are constructors used Cannot use elsewhere so would they help subclass? Name is specific & tied to superclass; how to use?
13
Inherit Such a Course Word Superclass constructor still needed by subclass… is-a Remember, subclass instance is-a superclass instance Instantiating superclass, too, when creating subclass Somehow need to initialize instances superclass part … but constructors not inherited (in same way)
14
Constructors Chains Java requires reusing superclass’s constructor Already initializes fields in superclass Provides us with way to maximize laziness Allows sharing constructor code within class, too Still want to avoid copying code Often do identical work initializing fields Constructors increase laziness through chaining
15
Constructors Chains Java requires reusing superclass’s constructor Already initializes fields in superclass Provides us with way to maximize laziness Allows sharing constructor code within class, too Still want to avoid copying code Often do identical work initializing fields Constructors increase laziness through chaining
16
Chains Explained
18
About To Get a Little Bumpy
19
Chaining Constructors Required Every Java constructor must be chained… this(…) or super(…) can be used to do chaining Class loops forever or will call superclass constructor But why is requirement new to you this lecture?
20
Chaining Constructors Required Every Java constructor must be chained… this(…) or super(…) can be used to do chaining Class loops forever or will call superclass constructor But why is requirement new to you this lecture?
21
Implicit Chaining Sounds Dirty If chaining not explicit made at constructor start Java adds implicit call to super() automatically Superclass needs constructor with no parameters… … which exists in Object (default superclass) But this empty constructor may not be in your classes
22
Constructors Not Inherited Constructors not inherited directly by subclass Calling superclass constructor relies upon chaining Only via chaining is constructor accessible in subclass Trying to use superclass constructor for new illegal Not all classes define constructor of their own If not defined, automatically get one with no params Implied chain used by this implicit constructor must To do this superclass must include usable constructor
23
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
24
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
25
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
26
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
27
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
28
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { super(type); color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
29
Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { super(type); color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }
30
Overriding Methods Subclass can redeclare inherited method Overloaded when different signature used Method is called overridden if signatures identical Specialize method in subclass by overriding Changed only for subclass & its subclasses Original used by superclass & other classes Actual method called determined by instance’s type
31
Polymorphism And Assignment
32
Types in Java In Java, use reference variables like silhouettes Compiler uses variable's type to see what is legal But (may) lose detail: this ignores instances type Important distinction between these two types Variable's type determines if code can compile Call method based upon instance's type
33
Inheritance Example public class SuperClass { public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getMyString() { return “sub ” + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); System.out.println(super1.otherMethod()); SubClass sub2 = super1;
34
Inheritance Example public class SuperClass { public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getMyString() { return “sub ” + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); System.out.println(super1.otherMethod()); SubClass sub2 = super1;
35
Your Turn Get into your groups and complete activity
36
For Next Lecture Read GT2.4 for Friday What is an abstract class? What is an abstract method? What is an interface ? Why would anyone use any of these? There is weekly assignment problem on Angel Due by 5PM Tuesday (via e-mail) Problem #2 graded using the provided JUnit tests
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.