Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015
Dec 31, 2015
Outline for Today1. Enums
2. Class inheritance
3. “Factoring out” code through inheritance
4. Typing using IS-A relationships
5. Access modifiers
First, let’s talk about Enums When a type can only take on a small, finite set of values we just want to call by name ...
Java provides a special kind of class called an enum.
Basically just a bunch of int-type fields – but the actual values are not accessible to the programmer.
Can be compared using the == operator.
Class Inheritance The main reason we have inheritance is so we don’t have to repeat code we’ve already written.
This is analogous to defining methods for common groups of statements.
class A
public void baz(){...}
class B
public void doh(){...}
int x;int y;
int x;int z;
public int foo() {...}public void bar() {...}
public int foo() {...}public void bar() {...}
Say we have these two class definitions ...
class A
public int foo() {...}public void bar() {...}
public void baz(){...}
class B
public int foo() {...}public void bar() {...}
public void doh(){...}
int x;int y;
int x;int z;
And these methods are defined the same way ...
class A extends S
public void baz(){...}
class B extends S
class S
int x;
public int foo() {...}public void bar() {...}
public void doh(){...}
int y; int z;
Use class inheritance to “factor out!”
class A
public int foo() {...}public void bar() {...}
public void baz(){...}
class B
public int foo() {...}public void bar() {...}
public void doh(){...}
int x;int y;
int x;int z;
Now what if these are declared with the same header but not defined the same way??
class A implements I
public int foo() {...}public void bar() {...}public void baz() {...}
class B implements I
interface I
int foo();void bar();
public int foo() {...}public void bar() {...}public void baz() {...}
int x;int y;
int x;int z;
Use an interface!(Sorry, the instance variablehas to stay)
class Athlete
public void run(){...}
class Refrigerator
interface Runner
void run();
public void run(){...}
Although this isn’t always applicable ...
class Nose
public void run(){...}
class Athlete
public void run(){...}
class Refrigerator
interface Runner
void run();
public void run(){...}
Although this isn’t always applicable ...
class Nose
public void run(){...}
Typing with IS-A Relationships If we have a variable of a certain type, we can put any object that IS that type in that variable.
DNAStrand head; // Can be StringDNAStrand, JoinedDNAStrand, etc.
Works with both classes and interfaces.
BUT We may call only those methods that are defined in that type!!!
class SuperDNAStrand extends StringDNAStrand {public void amazingMethod() {...}
}
DNAStrand head = new SuperDNAStrand();
head.amazingMethod();
BUT We may call only those methods that are defined in that type!!!
class SuperDNAStrand extends StringDNAStrand {public void amazingMethod() {...}
}
DNAStrand head = new SuperDNAStrand();
head.amazingMethod(); // compilation error
BUT We may call only those methods that are defined in that type!!!
class SuperDNAStrand extends StringDNAStrand {public void amazingMethod() {...}
}REMEMBER: IS-A relationships are transitive!!!
SuperDNAStrand is automatically “implementing” the
DNAStrand interface
Null Any reference-typed variable can take a null value:
◦ “empty” or pointing to no object.
“Referencing” a null-valued variable (e.g., calling a method on it) will result in the dreaded NullPointerException
class BadDNAStrand implements DNAStrand {DNAStrand head; // defaults to null
public char getBaseAt(int idx) {
return head.getBaseAt(idx); // null pointer exception
}
}
The Object Class Every Java class inherits from Object – even if you don’t say so!!
Therefore, and Object-type variable can hold any reference type.
BUT, you won’t be able to call any methods you have defined.
You won’t need the Object class very often, but sometimes you will see it come up as a workaround for certain type-checking issues.
Access Modifiers The public and private keywords are called access modifiers. They determine which other classes may access your fields or methods – critical for encapsulation.
There are two other levels of access: protected and default access (no keyword).
Access ModifiersKeyword Same class Same package Subclasses Errrbody
public Yes Yes Yes Yes
protected Yes Yes Yes NO
(none / default) Yes Yes NO NO
private Yes NO NO NO
Access Modifiers If you’re not sure what to use ...
◦ public for methods◦ private for fields
Also, methods declared in an interface are always public