1 / 22

Inheritance, part 1

Inheritance, part 1. COMP 401, Spring 2014 Lecture 8 2/4 /2014. Static Class Fields. Indicated by static modifier to field declaration. Can be accessed via the class name access modifiers can also be used to control visibility Contrast with instance fields

padma
Download Presentation

Inheritance, part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance, part 1 COMP 401, Spring 2014 Lecture 8 2/4/2014

  2. Static Class Fields • Indicated by static modifier to field declaration. • Can be accessed via the class name • access modifiers can also be used to control visibility • Contrast with instance fields • Each object has its own copy of an instance field • Value of the instance fields are the state of the object • Only one copy of the class field • Associated with the class as a whole • Common uses • As named constant values associated with the class • In this case, modify the declaration of the field with the keyword final • By convention, named constants are ALL_CAPS • As collective information associated with the class as a whole. • Avoid accessing static class fields through an object. • It does work, but it is misleading and not good style • lec8.ex1

  3. Static Class Methods • Methods associated with the class but that are not associated with a specific object. • No this reference within the function. • Common uses • Getters / Setters for static class fields • Helper / auxiliary functions • Factory methods for creating new instances • We’ll revisit this in more detail later later in the class. • lec8.ex2

  4. Motivating Enumerations • Often need to model part of an object as one value from a set of finite choices • Examples: • Suite of a playing card • Day of week • Directions of a compass • One approach is to use named constants • lec8.ex3 • Drawbacks of this approach • No type safety • No value safety

  5. Simple Java Enumerations • General syntax: access_typeenumEnumName {symbol, symbol, ...}; • Example: • public enum Genre {POP, RAP, JAZZ, INDIE, CLASSICAL} • Enumeration 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 qualified name required as in: Song.Genre.POP • Symbol names don’t have to all caps, but that is traditional • lec8.ex4

  6. Not so simple enumerations • Java enumerations are actually much more powerful than this. • Check out this tutorial for more: http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

  7. Recap of Interfaces • A “contract” for behavior. • Defined by a set of method signatures. • Acts as a data type. • No implementation. • Specific classes implement the interface

  8. Song and Video as Media public interface Media { intgetLengthInSeconds(); double getLengthInMinutes(); intgetRating(); void setRating(intnew_rating); String getName(); } We expect Song and Video to have methods matching those specified in Media. public class Song implements Media { .... public class Video implements Media { ....

  9. Is-A and casting • A class that implements an interface creates an “is-a” relationship between class data type and the interface data type. • A implements B => A “is a” B • Song is a Media • Video is a Media • Casting allowed across an is-a relationship. 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;

  10. Inheritance • What is inheritance in real life? • Characteristics / resources that you receive from your parents • Get these automatically. • 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 distinctly different. • Class inheritance more complex.

  11. 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 { intgetCompressedSize(); intgetUncompressedSize(); Media uncompress(); }

  12. Extension Creates Hierarchy Media • Is-A relationship is transitive up the hierarchy. extends Compressed Media Methods for both must be provided. public class Song implements CompressedMedia { ... 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. Casting from interface back to specific object type is allowed, but at runtime, if the object’s type does not actually match, a runtime exception will be thrown.

  13. Extension vs. Composition • Interface extension appropriate when additional methods make no sense without methods of the parent interface. • Alternatively, can compose multiple interfaces together as facets of an object.

  14. Extension vs. Composition public interface Compressed { intgetCompressedSize(); intgetUncompressedSize(); Media uncompress(); } public interface Media { intgetLengthInSeconds(); double getLengthInMinutes(); intgetRating(); void setRating(intnew_rating); String getName(); } Instead of extending Media, Compressed is a separate interface and Song implements both. public class Song implements Compressed, Media { ... Song s = new Song(); Media m = (Media) s; Compressed c = (Compressed) s; Song “is a” Media AND Song “is a” Compressed.

  15. Subinterfacing Odds and Ends • Multiple inheritance for interfaces is allowed. • A subinterface can extend more than one existing 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 define enumerations.

  16. 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 specific object provided to the method will be the appropriate type.

  17. Midterm Logistics • Strongly considering moving midterm to either be Thursday or Friday during recitation. • 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 accommodations • Let me know if this applies. • Take the test at learning services. • Drop it off at my office.

  18. Midterm • Part 1: Multiple 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 questions. • Example: • Which of these is NOT a valid built-in Java data type? • a) int • b) unsigned short • c) char • d) double • e) true

  19. Midterm • Part 2: Short answer / calculation • Evaluate small snippets of code • Example: • What is the value of the variable bar after the following code executes? int bar; int[] a = {1, 2, 3, 4, 5}; int[] b = a; b[2] = a[1]; bar = a[2];

  20. Midterm • Part III: Understanding Code • I’ll provide the code for one or more classes and then ask questions like: • Identify all of the instance fields of class A. • Identify all of the class methods of class A. • What is the return type of method m of class B?

  21. Midterm • Part IV: Writing Code • Asked to define one or more simple classes according to some specification. • May be provided with a partial implementation that you have to complete.

  22. Topics / Concepts • Basic Java syntax • Variable names, built-in data types • Reference types vs. value types • Arrays • Concepts of abstraction and encapsulation • JavaBeans conventions • Interfaces • Including extending interfaces covered today • Is-A relationship with interfaces • Enumerations • Iterator • Constructor Chaining / Method Overloading

More Related