Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?

Similar presentations


Presentation on theme: "Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?"— Presentation transcript:

1 Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?

2 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

3 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!

4

5 Sharing Among Classes

6 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

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

8 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); }

9 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  Starts with superclass which subclass extends extends  Automobile extends Vehicle extends  Truck extends Automobile extends  Car extends Automobile (& Vehicle ?)

10 extends extends Example extends extends extends public class Vehicle {…} public class Automobile extends Vehicle {…} public class Car extends Automobile {…} public class Truck extends Automobile {…} VehicleAutomobileCarTruck

11 extends extends Example  Each class extends exactly one class  extends  extends explicitly specifies superclass  If not stated, class defaults to subclass of Object VehicleAutomobileCarTruck

12 extends extends Example  Each class extends exactly one class  extends  extends explicitly specifies superclass  If not stated, class defaults to subclass of Object ObjectVehicleAutomobileCarTruck

13 extends extends Example  Extended by as many classes as heart desires  Automobile superclass of Car, Truck  Vehicle superclass of Automobile, Car, Truck ObjectVehicleAutomobileCarTruck

14 Superclass (& Subclass)  Every class is subclass of:  superclass  superclass’s superclass  superclass’s superclass’s superclass  superclass’s superclass’s superclass’s superclass, …  Object is-ais-a  Truck is-a Automobile is-a Vehicle is-a  So Truck is-a Object also

15 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

16 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);

17 What Gets Inherited And How?

18 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

19 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  super.methodName(…) used to call method supersuper  No multiples: super.super.methodName(…)  Cannot make less accessible when overriding  However, method can make more accessible

20 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  super.methodName(…) used to call method supersuper  No multiples: super.super.methodName(…)  Cannot make less accessible when overriding  However, method can make more accessible

21 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());

22 Your Turn  Get into your groups and complete activity

23 For Next Lecture  Read GT2.3 for Friday  How do constructors work with inheritance? is-a  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


Download ppt "Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?"

Similar presentations


Ads by Google