comp401sp14lec08inheritancept1
DESCRIPTION
first lectureTRANSCRIPT
-
Inheritance, part 1
COMP 401, Spring 2014 Lecture 8 2/4/2014
-
Sta;c Class Fields Indicated by sta$c modier to eld declara;on. Can be accessed via the class name
access modiers can also be used to control visibility Contrast with instance elds
Each object has its own copy of an instance eld Value of the instance elds are the state of the object
Only one copy of the class eld Associated with the class as a whole
Common uses As named constant values associated with the class
In this case, modify the declara;on of the eld with the keyword nal By conven;on, named constants are ALL_CAPS
As collec;ve informa;on associated with the class as a whole. Avoid accessing sta;c class elds through an object.
It does work, but it is misleading and not good style lec8.ex1
-
Sta;c Class Methods
Methods associated with the class but that are not associated with a specic object. No this reference within the func;on.
Common uses GeSers / SeSers for sta;c class elds Helper / auxiliary func;ons Factory methods for crea;ng new instances
Well revisit this in more detail later later in the class. lec8.ex2
-
Mo;va;ng Enumera;ons OWen need to model part of an object as one value from a set of nite choices Examples:
Suite of a playing card Day of week Direc;ons of a compass
One approach is to use named constants lec8.ex3
Drawbacks of this approach No type safety No value safety
-
Simple Java Enumera;ons General syntax:
access_type enum EnumName {symbol, symbol, ...};!
Example: public enum Genre {POP, RAP, JAZZ, INDIE, CLASSICAL}
Enumera;on name acts as the data type for the enumerated values. Enumerated values available as EnumName.symbol as in:
Genre.POP Outside of the class
Fully qualied name required as in: Song.Genre.POP Symbol names dont have to all caps, but that is tradi;onal lec8.ex4
-
Not so simple enumera;ons
Java enumera;ons are actually much more powerful than this.
Check out this tutorial for more: hSp://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html
-
Recap of Interfaces
A contract for behavior. Dened by a set of method signatures. Acts as a data type. No implementa;on.
Specic classes implement the interface
-
Song and Video as Media public interface Media { int getLengthInSeconds(); double getLengthInMinutes(); int getRa;ng(); void setRa;ng(int new_ra;ng); String getName(); }
public class Song implements Media { ....
public class Video implements Media { ....
We expect Song and Video to have methods matching those specied in Media.
-
Is-A and cas;ng A class that implements an interface creates an is-a rela;onship between class data type and the interface data type. A implements B => A is a B
Song is a Media Video is a Media
Cas;ng allowed across an is-a rela;onship.
9
Song s = new Song(); Media m; m = (Media) s;
Video v = new Video(); Media m; m = (Media) v;
Video v = new Video(); Song s; s = (Song) v;
-
Inheritance
What is inheritance in real life? Characteris;cs / resources that you receive from your parents Get these automa;cally. Part of who you are.
Similar idea in object-oriented programming. In Java, concept of inheritance applied to both interfaces and classes. Both signaled by the keyword extends Similar in concept, but details are dis;nctly dierent.
Class inheritance more complex.
-
Extending Interfaces Adds methods to contract. Original:
parent interface, super interface New:
subinterface, child interface, extended interface Created by using the extends keyword.
public interface CompressedMedia extends Media { int getCompressedSize(); int getUncompressedSize(); Media uncompress(); }
-
Extension Creates Hierarchy Is-A rela;onship is
transi;ve up the hierarchy.
Media
Compressed Media
extends
public class Song implements CompressedMedia { ...
Methods for both must be provided.
Song s = new Song(); CompressedMedia cm = (CompressedMedia) s; Media m = (Media) s; Song s2 = (Song) m;
OK because s is a Compressed Media
OK because s is a Media by virtue of extension.
Cas$ng from interface back to specic object type is allowed, but at run$me, if the objects type does not actually match, a run$me excep$on will be thrown.
-
Extension vs. Composi;on Interface extension appropriate when addi;onal methods make no sense without methods of the parent interface.
Alterna;vely, can compose mul;ple interfaces together as facets of an object.
-
Extension vs. Composi;on public interface Compressed { int getCompressedSize(); int getUncompressedSize(); Media uncompress(); }
public interface Media { int getLengthInSeconds(); double getLengthInMinutes(); int getRa;ng(); void setRa;ng(int new_ra;ng); String getName(); }
public class Song implements Compressed, Media { ...
Instead of extending Media, Compressed is a separate interface and Song implements both.
Song s = new Song(); Media m = (Media) s; Compressed c = (Compressed) s;
Song is a Media AND Song is a Compressed.
-
Subinterfacing Odds and Ends
Mul;ple inheritance for interfaces is allowed. A subinterface can extend more than one exis;ng interface.
In this case, just a union of all methods declared in all of the parent interfaces. Plus, of course, the ones added by the subinterfaces.
Interfaces can dene enumera;ons.
-
Assignment 3
Pixel iterator should NOT make a copy of the pixels in the frame. SHOULD maintain a reference to the frame and any info needed to keep track of the traversal.
setPixel method will require a cast You can assume that the specic object provided to the method will be the appropriate type.
-
Midterm Logis;cs
Strongly considering moving midterm to either be Thursday or Friday during recita;on. Will talk to TAs on Wed. evening and will have a decision posted that night.
Closed book No need for exam book or scantron Learning accommoda;ons Let me know if this applies. Take the test at learning services. Drop it o at my oce.
-
Midterm Part 1: Mul;ple choice, true/false Basic Java language syntax OO concepts More than one answer may be correct.
Think of it as a group of related true/false ques;ons. Example:
Which of these is NOT a valid built-in Java data type? a) int b) unsigned short c) char d) double e) true
-
Midterm
Part 2: Short answer / calcula;on Evaluate small snippets of code
Example: What is the value of the variable bar aWer the following code executes? int bar; !int[] a = {1, 2, 3, 4, 5}; !int[] b = a; !b[2] = a[1]; !bar = a[2]; !
-
Midterm
Part III: Understanding Code Ill provide the code for one or more classes and then ask ques;ons like: Iden;fy all of the instance elds of class A. Iden;fy all of the class methods of class A. What is the return type of method m of class B?
-
Midterm
Part IV: Wri;ng Code Asked to dene one or more simple classes according to some specica;on. May be provided with a par;al implementa;on that you have to complete.
-
Topics / Concepts Basic Java syntax
Variable names, built-in data types Reference types vs. value types Arrays Concepts of abstrac;on and encapsula;on JavaBeans conven;ons Interfaces
Including extending interfaces covered today Is-A rela;onship with interfaces
Enumera;ons Iterator Constructor Chaining / Method Overloading