CS/ENGRD 2110 Fall2017 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.
Object Oriented Programming
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
Inheritance Inheritance Reserved word protected Reserved word super
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
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 Exercise /* A lockbox can be open or closed. If closed, only a valid password will open the box. Once the box is open, the contents can be retrieved.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
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.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
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.
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.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Lecture 5:Interfaces and Abstract Classes
CS Feb 2010 Customizing a class
OOP: Encapsulation &Abstraction
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.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS 302 Week 11 Jim Williams, PhD.
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 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Java Inheritance.
Java Programming Language
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
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 5: Local vars; Inside-out rule; constructors
Chapter 11 Inheritance and Polymorphism Part 1
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
CS Feb 2011 Customizing a class
Building Java Programs
Presentation transcript:

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

Announcements A1 Due Thursday 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) Methods are the operations on objects of that class. Fields allow objects to contain data (L3)

Class House House@af8 House bdrs 3 baths 1 public class House { private int bdrs; // number of bedrooms, >= 0. private int baths; // number of bathrooms, in 1..5 /** Constructor: number of bedrooms b1, number of bathrooms b2 * Prec: b1 >= 0, 0 < b2 <= 5 */ public House(int b1, int b2); /** Return number of bedrooms */ public int getBeds() { return bdrs; } /** Return number of bathrooms */ public int getBaths() { return baths; House@af8 House bdrs 3 baths 1 House(…) getBeds() getBaths() setBeds(…) setBaths(…) toString() equals(Object) hashCode() Contains other methods!

Class Object

Class Object: the superest class of all public class House { private int bdrs; // number of bedrooms, >= 0. private int baths; // number of bathrooms, in 1..5 /** Constructor: number of bedrooms b1, number of bathrooms b2 * Prec: b1 >= 0, 0 < b2 <= 5 */ public House(int b1, int b2); /** Return number of bedrooms */ public int getBeds() { return bdrs; } /** Return number of bathrooms */ public int getBaths() { return baths; extends Object { Java: Every class that does not extend another extends class Object. House@af8 House bdrs 3 baths 1 House(…) getBeds() getBaths() setBeds(…) setBaths(…) 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.

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 JFrame C: subclass of JFrame JFrame: superclass of C C inherits all methods that are in a JFrame hide() show() setTitle(String) getTitle() getX() getY() setLocation(int, int) getWidth() getHeight() … C Object has 2 partitions: one for JFrame methods, one for C methods

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 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 this keyword: 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 public class Apartment extends House { private int floor; private Apartment downstairs; //constructor public Apartment(int floor, Apartment downstairs) { floor= floor; downstairs = downstairs; } public class Apartment extends House { private int floor; private Apartment downstairs; //constructor public Apartment(int floor, Apartment downstairs) { this.floor= floor; this.downstairs = downstairs; } this avoids overshadowed field name Inside-out rule shows that field x is inaccessible!

Overriding methods Apartment@af8 toString() equals(Object) hashCode() Object House bdrs 3 baths 1 House(…) getBeds() getBaths() setBeds(…) setBaths(…) Apartment floor 2 Apartment(…) isBelow(…) upstairs Apartment@f34 Object defines a method toString() that returns the name of the object Apartment@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 Apartment@af8 Object toString() equals(Object) hashCode() Object House bdrs 3 baths 1 House(…) getBeds() getBaths() setBeds(…) setBaths(…) Apartment floor 2 Apartment(…) isBelow(…) upstairs Apartment@f34 public class Apartment{ … /** Return a representation of an Apartment*/ @Override public String toString() { return "" +(getBeds() +getBaths()) + " 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 { … }

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. You should make a method static if the body does not refer to any field or method in the object.

An Example /** = “this object is below”. Pre: a is not null. */ public boolean isBelow(Apartment a){ return this == a.downstairs; } /** = “a is below b”. Pre: b and c are not null. */ public static boolean isBelow(Apartment b, Apartment a){ return b == a.downstairs; }

Referencing a static method static: there is only one copy of the method. It is not in each object A@af H bdrs 2 floor 4 dstrs baths 1 A A@b4 isBelow(A) isBelow(A) isBelow(Apartment, Apartment) Container for Apartment contains: objects , static components public static void main(String[] args) { Apartment.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 Apartment extends House { public static int numAps; // number of Apartments created } /** Constructor: */ public Apartment(…) { … numAps= numAps + 1; } A@af H bdrs 2 floor 4 dstrs baths 1 A A@b4 To have numAps contain the number of objects of class Apartment that have been created, simply increment it in constructors. numAps 2 numAps stored in the Container for Apartment To access: Apartment.numAps

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 Box for WhiteHouse