Inheritance (Discussion 4) (Kế thừa trong Java) - Christine Zhou

UN
Uni24h
0 lượt tải
19
0

Nội dung tài liệu

Trang 2

Agenda - Announcements - Review: Inheritance - Problem 1 - Review: More Inheritance - Problem 2 - Review: Static/Dynamic Types - Problem 3

Trang 3

Announcements - Office hours was crowded… - Make sure to get started early and go to office hours earlier for help! - Piazza questions: give us more information! - What is the error that is being outputted? What have you tried to fix the error? - Git-bug - Proj0 due Monday 11:59PM - See late policy on the course website - HW2 due this Friday 11:59PM, go to OH early!! - Guerrilla section planned for this weekend - Saturday, 1-3PM in the labs - Midterm next week on Wednesday 9/27 8-10PM - Discussion survey: tinyurl.com/czdisc4

Trang 4

Review: Inheritance public class Fish { public class Goldfish { String name; String name; public Fish(String name) public Goldfish(String name) {...} {...} Public void eat() {...} public void eat() { public void blink() {...} print(“bloop”); ... } } public void blink() {...} }

Trang 5

Review: Inheritance - extends (can extend only one class) - Inherits all instance variables and methods - Constructor is not inherited, but there will be an implicit call to super( ) in the beginning of the subclass’s constructor (need to make the Fish before making the Goldfish) constructor() { constructor() { this.hello = 10; super(); SAME! } this.hello = 10; } constructor() { constructor() { this.hello = 10; super(10); DIFFERENT! } this.hello = 10;

Trang 6

Review: Inheritance public class Fish { public class Goldfish extends Fish { String name; public Goldfish(String name) { public Fish(String name) super(name); // need to {...} have this! ... Public void eat() {...} } public void blink() {...} public void eat() { } print(“bloop”); super.eat(); } }

Trang 7

1 Creating Cats Given the Animal class, fill in the definition of the Cat class so that it makes a "Meow!" noise when greet() is called. Assume this noise is all caps for kittens (less than 2 years old). How similar are Cats with Animals? How do we take advantage of the extends? class Cat extends Animal { }

Trang 8

Review: More Inheritance - Anything that lives in water must have these two methods: float(), swim() - If we have a Fish or SeaOtter class, it must have these! - How do we guarantee this?

Trang 9

Review: More Inheritance - Let’s define an interface, all classes that implement this interface must have all the methods - Can implement multiple interfaces public interface WaterBeing { public class Fish implements WaterBeing { public void swim(); public Fish() {...} public void float(); @Override } public void swim() {...} @Override

Trang 10

Review: More Inheritance - Can also have abstract classes - A normal class with one or more “abstract” things (method/variable) - Any subclass must implement the abstract things, or it will also be an abstract class - Cannot be instantiated (how can you instantiate something when the behavior is abstract?) public abstract class Fish { public class Goldfish extends Fish { ... ... public abstract void bloop(); public void bloop() {...} public void swim() {...} } }

Trang 14

Review: Static vs. Dynamic Types - Fish f = new Goldfish() - Fish is the static type, this is what Java looks at during compilation - Goldfish is the dynamic type, this is what Java looks at during runtime - The dynamic type can be any subclass of the static type - Does Fish b = new Goldfish() work? - How about Goldfish c = new Fish()? - Casting changes the static type of an object temporarily - Fish d = new Goldfish(); - ((Goldfish) d).bloop(); // static type of d is Goldfish for this line - d.bloop(); // static type of d is Fish

Trang 15

Review: Static vs. Dynamic Types - Method and field lookup in non-static contexts: - During compilation: - Check if the method/field exists in the static type - If doesn’t exist, go to the superclass and repeat - If no superclass, compile time error - During runtime: - Run the method/use the field in the dynamic type (dynamic method selection), if not in the current class, go to the parent, and then the parent…

Trang 16

3 Raining Cats & Dogs Cat nyan = new Animal(“Nyan Cat”, 5); Animal a = new Cat(“Olivia Benson”, 3); a = new Dog(“Fido”, 7); System.out.println(a.greet()); a.playFetch(); Dog d1 = a; Dog d2 = (Dog) a; d2.playFetch(); (Dog) a.playFetch();

Trang 17

3 Raining Cats & Dogs Animal imposter = new Cat(“Pedro”, 12); Dog fakeDog = (Dog) imposter; Cat failImposter = new Cat(“Jimmy”, 21); Dog failDog = (Dog) failImposter;

Trang 18

3 Raining Cats & Dogs class Cat extends Animal { What would the following output? public Cat(String name, int age) { Animal a = new Cat(“Chris”, super(name, age); 4); this.noise = “Meow!”; a.greet(); } public String greet() { System.out.println(“I am a cat!”); super.greet(); } }

Trang 19

Wondering about an edge case involving inheritance or static/dynamic typing? Try it out on your own! Credit to Michael Ju

Hiển thị 19 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!