Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015."— Presentation transcript:

1 Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015

2 Outline for Today 1.Enums 2.Class inheritance 3.“Factoring out” code through inheritance 4.Typing using IS-A relationships 5.Access modifiers

3 First, let’s talk about Enums Motivating example: enums.v1

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

5 Enum Syntax and Type Rules enums.v2

6 Nested Enums and Switch Statements enums.v3

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

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

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

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

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

12 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 variable has to stay)

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

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

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

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

17 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

18 BUT We may call only those methods that are defined in that type!!! class SuperDNAStrand extends StringDNAStrand { public void amazingMethod() {...} } R EMEMBER : IS-A relationships are transitive!!! SuperDNAStrand is automatically “implementing” the DNAStrand interface

19 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 } }

20 Null Value vs. Null String String s1 = null; String s2 = “”; // s1 is not the same as s2!

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

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

23 Access Modifiers KeywordSame classSame packageSubclassesErrrbody public Yes protected Yes NO (none / default)Yes NO private YesNO

24 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

25 Four Pillars of Object Orientation I.Encapsulation access modifiers II.Modularity classes III.Inheritance IV.Polymorphism interfaces overriding overloading


Download ppt "Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015."

Similar presentations


Ads by Google