problem of the day what is the smallest positive integer that cannot be defined in less than...
TRANSCRIPT
![Page 1: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/1.jpg)
Problem of the Day
What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
![Page 2: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/2.jpg)
Problem of the Day
What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Syllables in highlighted phrase: 24
![Page 3: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/3.jpg)
Problem of the Day
What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Syllables in highlighted phrase: 24 If it existed, this defines number in 24
syllables Therefore no such number exists!
![Page 4: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/4.jpg)
LECTURE 10:INHERITANCE
![Page 5: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/5.jpg)
Sharing Among Classes
Classes often share actions & data Writing code once is laziest option
Cutting-and-pasting still requires effort Increase bugs with multiple copies of code
Two ways to not rewrite code:
Inheritance&
Composition
![Page 6: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/6.jpg)
Composition
Used when there is “has-a” relationship Student has a name Full name has a first name Car has an engine 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
![Page 7: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/7.jpg)
Composition Example
public class FullName {private String firstName;private String lastName;// constructor, getters & setters also here
}
public class Student {private FullName name;// Brevity is the soul of wit- Shakespeare
public String getFirstName() { return name.getFirstName();}
public void setFirstName(String newName) { name.setFirstName(newName);}
![Page 8: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/8.jpg)
Composition Example
public class FullName {private String firstName;private String lastName;// constructor, getters & setters also here
}
public class Student {private FullName name;// Brevity is the soul of wit- Shakespeare
public String getFirstName() { return name.getFirstName();}
public void setFirstName(String newName) { name.setFirstName(newName);}
![Page 9: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/9.jpg)
Inheritance
“Is-a” relationships implemented via inheritance Automobile is a Vehicle Car is a Vehicle Truck is an Automobile Car is an Automobile
Starts with superclass which subclass extends Automobile extends Vehicle Truck extends Automobile Car extends Automobile (& Vehicle?)
![Page 10: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/10.jpg)
extends Example
public class Vehicle {…}public class Automobile extends Vehicle {…}public class Car extends Automobile {…}public class Truck extends Automobile {…}
Vehicle
Automobile
Car Truck
![Page 11: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/11.jpg)
extends Example
Each class extends exactly one class extends explicitly specifies superclass If not stated, class defaults to subclass of Object Vehicle
Automobile
Car Truck
![Page 12: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/12.jpg)
extends Example
Each class extends exactly one class extends explicitly specifies superclass If not stated, class defaults to subclass of Object Object
Vehicle
Automobile
Car Truck
![Page 13: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/13.jpg)
extends Example
Extended by as many classes as heart desires Automobile superclass of Car, Truck Vehicle superclass of Automobile, Car, Truck
Object
Vehicle
Automobile
Car Truck
![Page 14: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/14.jpg)
Superclass (& Subclass)
Every class is subclass of: superclass superclass’s superclass superclass’s superclass’s superclass superclass’s superclass’s superclass’s
superclass, … Object
Truck is-a Automobile is-a Vehicle So Truck is-a Object also
![Page 15: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/15.jpg)
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
![Page 16: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/16.jpg)
Inheritance Example
public class SuperClass { protected String str = “PARENT”; public String getMyString() { return “SUPER”; }}public class SubClass extends SuperClass { public String getFullString() { return “sub ” + getMyString(); } public SubClass() { str = “kid”; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super.getMyString()); System.out.println(sub.getFullString()); System.out.println(sub.str); System.out.println(super.str);
![Page 17: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/17.jpg)
What Gets Inherited And How?
Inheritance is part of extends and automatic DON’T RETYPE IN SUBCLASS
Redeclaring fields leads to bad things Yields way of specializing when redeclaring
methods
![Page 18: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/18.jpg)
Overriding Methods
Subclass can redeclare inherited method Overloaded when different signature used Method is called overridden if signatures
identical Specialize method execution in
subclass this way Changed only for subclass & its subclasses Original used by superclass & other classes Actual method called determined by
instance’s type
![Page 19: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/19.jpg)
Overriding Methods
Call overriden method as defined in superclass Only in subclass overriding the method If method is not overriden then this is not
needed super.methodName(…) used to call method No multiples: super.super.methodName(…)
Cannot make less accessible when overriding However, method can make more accessible
![Page 20: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/20.jpg)
Overriding Methods
Call overriden method as defined in superclass Only in subclass overriding the method If method is not overriden then this is not
needed super.methodName(…) used to call method No multiples: super.super.methodName(…)
Cannot make less accessible when overriding However, method can make more accessible
![Page 21: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/21.jpg)
Inheritance Example
public class SuperClass { public String getMyString() { return “SUPER”; }}public class SubClass extends SuperClass { public String getMyString() { return “sub ”; } 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.getMyString());
![Page 22: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/22.jpg)
Your Turn
Get into your groups and complete activity
![Page 23: Problem of the Day What is the smallest positive integer that cannot be defined in less than twenty-five syllables?](https://reader030.vdocuments.mx/reader030/viewer/2022032722/56649f4e5503460f94c6f65a/html5/thumbnails/23.jpg)
For Next Lecture
Read GT2.3 for Friday How do constructors work with inheritance? What does is-a mean as far as variables? To what does polymorphism translate?
There is weekly assignment problem on Angel Due by 5PM next Tuesday (via e-mail) Will be graded using the provided JUnit
tests