0% found this document useful (0 votes)
29 views10 pages

Discussion 4

Uploaded by

DAR RYL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

Discussion 4

Uploaded by

DAR RYL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Discussion 4

Christine Zhou
Agenda
Announcements

Review

Worksheet (1, 2 for sure)


Announcements
● Proj 1A due last Friday (2/3)
● Guerrilla Section 1 happened! (2/4)
○ Worksheet/solutions posted on Piazza
● Proj 1B due this Friday (2/10) at 11:59 PM
● Lab is required this week! Proj 1 Code Reviews
● Attendance: shoutkey.com/found
Inheritance!
● interface, what is it?
○ Specification of what something is allowed to do, not how to do it!
○ Classes have to implement the interface (and therefore fill in all the methods)
○ Interfaces can have default methods, that are the same for all classes that
implement the interface

public interface Swimmer {


public void swim();
default public void bloop() {
System.out.println(“bloop”);
}

}
Inheritance!
● extends, what does it do?
○ Keyword that allows classes to extend other classes
○ All instance/static variables, methods, nested classes inherited
○ Constructor is not inherited, though super() will be called at the beginning of the
constructor implicitly (see next slide)
● Both of these represent is-a relationships, not has-a! (they represent
ideas like Goldfish is a Fish, not Goldfish has a tail!)

public class Fish { public class Goldfish extends


Fish {

… ...

} }
constructor() { constructor() {
this.hello = 10; super();
}
this.hello = 10;
}
● These are equivalent!

constructor() { constructor() {
this.hello = 10; super(10);
}
this.hello = 10;
}
● These are not equivalent!
Overloaded vs. Overridden
public class Fish { public class Goldfish
extends Fish {

...
public void bloop() {} public void
bloop() {}
} }
● Example of overridden methods! bloop in both the Goldfish and Fish
class
● Usually use “@Override” right above the method for clarity

public class Goldfish extends Fish {



public void eat() {}
public void eat(food) {}
Static vs. Dynamic
● Static type is determined at declaration, and never changes
(unless you re-declare)
○ Also known as compile-time type
● Dynamic type is determined at instantiation
○ Also known as runtime type
○ “new” keyword
○ Can change it to something else!
● Fish fishy = new Fish();
○ Static and dynamic type of fishy is Fish
● fishy = new Goldfish();
○ Static type of fishy is Fish, but dynamic type is Goldfish
● Good example (from lecture)
Dynamic Method Selection
● Occurs when a method is overridden (what does that mean
again?)
○ Overridden means there is a method with the same signature (name, args, return
type) in a class and the class/interface is extends/implements
● Chooses the method corresponding to the dynamic type (what
does that mean again?)
○ Dynamic type is determined at declaration, “new” keyword
Java’s Type Checking
● Checks types during compilation
● When calling methods and there is no overriding, use the static type
of the variable
○ Fish fishy = new Goldfish();
○ fishy.swim(); do we look at static or dynamic type when looking for “swim”?
● Variable assignment is based on compile-time type!
○ If Goldfish extends Fish, is this valid? Fish fishy = new Goldfish();
○ How about this? Goldfish fishy = new Fish();
● Can cast variables to change the compile-time type of the variable
(though can lead to more errors…)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy