Top Banner
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Class InheritanceUNC-CHAPEL H I LL

COMP 401

BR IAN CR ISTANTE

5 FEBRUARY 2015

Page 2: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Outline for Today1. Enums

2. Class inheritance

3. “Factoring out” code through inheritance

4. Typing using IS-A relationships

5. Access modifiers

Page 3: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

First, let’s talk about EnumsMotivating example: enums.v1

Page 4: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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.

Page 5: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Enum Syntax and Type Rulesenums.v2

Page 6: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Nested Enums and Switch Statementsenums.v3

Page 7: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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.

Page 8: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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 ...

Page 9: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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 ...

Page 10: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 11: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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??

Page 12: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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)

Page 13: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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(){...}

Page 14: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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(){...}

Page 15: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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.

Page 16: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 17: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 18: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 19: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

}

}

Page 20: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Null Value vs. Null String String s1 = null;

String s2 = “”;

// s1 is not the same as s2!

Page 21: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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.

Page 22: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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).

Page 23: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 24: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

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

Page 25: Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.

Four Pillars of Object OrientationI. Encapsulation

access modifiers

II. Modularityclasses

III. Inheritance

IV. Polymorphisminterfacesoverridingoverloading