Class Notes (1,100,000)
US (490,000)
Cornell (1,000)
CS (100)
CS 2110 (30)
Gries (30)
Lecture 4

CS 2110 Lecture Notes - Lecture 4: Local Variable, Class Variable


Department
Computer Science
Course Code
CS 2110
Professor
Gries
Lecture
4

This preview shows half of the first page. to view the full 3 pages of the document.
Lecture 4 - Class Hierarchy
oReview - Big Ideas
Type: A set of values and operations on them (L1)
(int: +, -, *, /, %, etc.)
Classes define new types (L2)
oA note on method specifications
Method specs SHOULD NOT mention fields
Why? Because the fields are hidden
So why not just make the fields public?
Because you might have different ways of constructing new
instances of the class that don’t use certain fields
So for getHour(), if we construct the class using only minutes, then
there is no hour field!
oToday: Class W (for Worker)
Methods:
Public W(String n, int s, W b)
Constructor with name n, Ssn s, Boss b
Public String getLname()
Returns Last Name
Public String getSsn()
Returns last 4 digits of Ssn
Public W getBoss()
Returns Boss
Public void setBoss(W b)
Changes Boss to b
So if these are our only methods, what happens if we try to use toString()?
It by default returns the id stored in our object
Ex: W Prez = new W(“Martinez”, 123456789, null);
Prez.toString() returns the id of the object stored in Prez!!
But if we haven’t implemented a method toString(), how
could this be possible!
No matter our class, the Class Object is a superclass for all of
them, the superest class
Has some default methods, like toString()
oInheritance of superclasses
This needs to designed to make sense
Don’t just use extends to get hidden members
So if class A extends B, A SHOULD BE AN ACTUAL PHYSICAL
EXTENSION OF B
Example: Elephant would extend Animal
Example
Triangle extends Shape
PHDTester DOES NOT EXTEND PHD
It isn’t a type of PHD
When I say type right now, I mean like an
example
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version