CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components http://cs.cornell.edu/courses/cs2110.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
CS/ENGRD 2110 SPRING 2014 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Java Implementation: Part 4 Software Construction Lecture 9.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Lecture 5:Interfaces and Abstract Classes
OOP: Encapsulation &Abstraction
Data Structures and Algorithms revision
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Lecture 10 Review of classes
Object-oriented Programming in Java
CS/ENGRD 2110 Fall 2017 Lecture 3: Fields, getters and setters, constructors, testing
Java Inheritance.
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Java Programming Language
Polymorphism and access control
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Building Java Programs
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Chapter 11 Inheritance and Polymorphism
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Building Java Programs
Presentation transcript:

CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components http://cs.cornell.edu/courses/cs2110

Announcements A1 Due Friday A2 Out Today

Where am I? Big ideas so far. Java variables have types (L1) A type is a set of values and operations on them (int: +, -, *, /, %, etc.) Classes define new types (L2) and define the contents of each object of the class. Methods are the operations on objects of that class. Fields allow objects to contain data (L3)

Class House House@af8 House nBed 3 nBath 1 House(…) getNumBed() public class House { private int nBed; // number of bedrooms, >= 0. private int nBath; // number of bathrooms, in 1..5 /** Constructor: bed is number of bedrooms, * bath is number of bathrooms * Prec: bed >= 0, 0 < bath <= 5 */ public House(int bed, int bath) { nBed= bed; nBath= bath; } /** Return no. of bedrooms */ public int getNumBed() { return nBed; … House@af8 House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…) setNumBath(…) toString() equals(Object) hashCode() Contains other methods!

Class Object

Class Object: the superest class of all public class House { private int nBed; // number of bedrooms, >= 0. private int nBath; // number of bathrooms, in 1..5 /** Constructor: bed is number of bedrooms, * bath is number of bathrooms * Prec: bed >= 0, 0 < bath <= 5 */ public House(int bed, int bath) { nBed= bed; nBath= bath; } /** Return no. of bedrooms */ public int getNumBed() { return nBed; … extends Object { Java: Every class that does not extend another class extends class Object. House@af8 House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…) toString() equals(Object) hashCode() Object Before this, demo Object using Meta and Empty.

Class Object: the superest class of all public class House { private int nBed; // number of bedrooms, >= 0. private int nBath; // number of bathrooms, in 1..5 /** Constructor: bed is number of bedrooms, * bath is number of bathrooms * Prec: bed >= 0, 0 < bath <= 5 */ public House(int bed, int bath) { nBed= bed; nBath= bath; } /** Return no. of bedrooms */ public int getNumBed() { return nBed; … extends Object { Java: Every class that does not extend another class extends class Object. House@af8 House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…) toString() equals(Object) hashCode() Object We often omit the Object partition to reduce clutter; we know that it is always there. Before this, demo Object using Meta and Empty.

Class Object: the superest class of all public class House { private int nBed; // number of bedrooms, >= 0. private int nBath; // number of bathrooms, in 1..5 /** Constructor: bed is number of bedrooms, * bath is number of bathrooms * Prec: bed >= 0, 0 < bath <= 5 */ public House(int bed, int bath) { nBed= bed; nBath= bath; } /** Return no. of bedrooms */ public int getNumBed() { return nBed; … extends Object { Java: Every class that does not extend another class extends class Object. House@af8 toString() equals(Object) hashCode() Object We often omit the Object partition to reduce clutter; we know that it is always there. Before this, demo Object using Meta and Empty. House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…)

Classes can extend other classes We saw this in L2! /** An instance is a subclass of JFrame */ public class C extends javax.swing.JFrame { } C@6667f34e C: subclass of JFrame JFrame: superclass of C C inherits all methods that are in a JFrame Object equals() toString() … JFrame hide() show() setTitle(String) getTitle() object has 3 partitions: for Object components, for JFrame components, for C components getWidth() getHeight() … getX() getY() setLocation(int, int) C

Classes can extend other classes You also saw this in the tutorial for this week's recitation There are subclasses of Exception for different types of exceptions NFE@2 Object Throwable Exception NumberFormatException

Accessing superclass things Subclasses are different classes Public fields and methods can be accessed Private fields and methods cannot be accessed Protected fields can be access by subclasses

Keywords: this // has no effect! public class House { private int nBed; // number of bedrooms, >= 0. private int nBath; // number of bathrooms, in 1..5 /** Constructor: */ public House(int nBed, int nBath) { nBed= nBed; nBath= nBath; } this.nBed= nBed; this.nBath= nBath; // has no effect! Inside-out rule shows that field nBed is inaccessible!  this avoids overshadowed field names this evaluates to the name of the object in which it occurs Makes it possible for an object to access its own name (or pointer) Example: Referencing a shadowed class field

A Subclass Example public class House { private int nBed; // num bedrooms, >= 0 private int nBath; // num bathrooms, in 1..5 /** Constructor: bed is number of bedrooms * bath is number of bathrooms * Prec: bed >= 0, 0 < bath <= 5 */ public House(int bed, int bath) { nBed= bed; nBath= bath; } public int getNumBed() { return nBed; … public class Apt extends House { private int floor; private Apt downstairsApt; public Apt(int floor, Apt downstairs) { this. floor= floor; downstairsApt= downstairs; } }

Overriding methods Apt@af8 toString() equals(Object) hashCode() Object House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…) Apt floor 2 Apt(…) isBelow(…) downstairsApt Apartment@f34 Object defines a method toString() that returns the name of the object Apt@af8 Java Convention: Define toString() in any class to return a representation of an object, giving info about the values in its fields. New definitions of toString() override the definition in Object.toString()

Overriding methods Apt@af8 Object toString() equals(Object) hashCode() House nBed 3 nBath 1 House(…) getNumBed() getNumBath() setNumBed(…) Apt floor 2 Apt(…) isBelow(…) upstairsApt Apartment@f34 public class Apt{ … /** Return a representation of an Apartment*/ @Override public String toString() { return "" + (getNumBed() + getNumBath()) + " room apartment on " + floor + "th floor"; } a.toString() calls this method toString()

When should you make a subclass? The inheritance hierarchy should reflect modeling semantics, not implementation shortcuts A should extend B if and only if A “is a” B An elephant is an animal, so Elephant extends Animal A car is a vehicle, so Car extends Vehicle An instance of any class is an object, so AnyClass extends java.lang.Object Don’t use extends just to get access to protected fields!

When should you make a subclass? Which of the following seem like reasonable designs? Triangle extends Shape { … } PHDTester extends PHD { … } BankAccount extends CheckingAccount { … }

When should you make a subclass? Which of the following seem like reasonable designs? Triangle extends Shape { … } Yes! A triangle is a kind of shape. PHDTester extends PHD { … } No! A PHDTester “tests a” PHD, but itself is not a PHD. BankAccount extends CheckingAccount { … } No! A checking account is a kind of bank account; we likely would prefer: CheckingAccount extends BankAccount { ... }

Static Methods Most methods are instance methods: every instance of the class has a copy of the method There is only one copy of a static method. There is not a copy in each object. Make a method static if the body does not refer to any field or method in the object.

An Example /** returns true if this object is below Apt a”. Pre: a is not null. */ public Boolean isBelow(Apt a){ return this == a.downstairsApt; } /** returns true if Apt b is below Apt a Pre: b and c are not null. */ public static boolean isBelow(Apt b, Apt a){ return b == a.downstairsApt; }

Referencing a static method static: there is only one copy of the method. It is not in each object Apt@af House nBed 2 floor 4 dstrs nBath 1 Apt Apt@b4 isBelow(Apt) isBelow(Apt) isBelow(Apt, Apt) Container for Apartment contains: objects , static components { a= new Apt(...); b= new Apt(...); if (a.isBelow(b)) ... if (Apt.isBelow(a, b)) ... }

Good example of static methods java.lang.Math http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html Or find it by googling Java 8 Math

What are static fields good for? There is only one copy of a static method. There is not a copy in each object. There is only one copy of a static field. There is not a copy in each object. What are static fields good for?

Use of static variables: Maintain info about created objects public class Apt extends House { public static int numApt; // number of Apartments created } /** Constructor: */ public Apt(…) { … numApt= numApt + 1; } Apt@af H nBed 2 floor 4 dstrs nBath 1 Apt Apt@b4 To have numApt contain the number of objects of class Apartment that have been created, simply increment it in constructors. numApt 2 numAps stored in the Container for Apartment To access: Apartment.numApt

Class java.awt.Color uses static variables An instance of class Color describes a color in the RGB (Red-Green-Blue) color space. The class contains about 20 static variables, each of which is (i.e. contains a pointer to) a non-changeable Color object for a given color: public static final Color black= …; public static final Color blue= …; public static final Color cyan= new Color(0, 255, 255); public static final Color darkGray= …; public static final Color gray= …; public static final Color green= …; …

Uses of static variables: Implement the singleton pattern Only one WhiteHouse can ever exist. public class WhiteHouse extends House{ private static final WhiteHouse instance= new WhiteHouse(); private WhiteHouse() { } // ... constructor public static WhiteHouse getInstance() { return instance; } // ... methods WhiteHouse@x3k3 WH … instance WhiteHouse@x3k3 Container for WhiteHouse