1 / 27

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu. Announcements. We will return exams some time next week. Agenda. Today: Inheritance uses of inheritance overuse of inheritance Coming up:

ethel
Télécharger la présentation

CSE115: Introduction to Computer Science I

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. CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

  2. Announcements • We will return exams some time next week.

  3. Agenda • Today: • Inheritance • uses of inheritance • overuse of inheritance • Coming up: • Inheritance • overriding of methods (full, partial, none) • Primitives • More control structures

  4. Last time • We saw two classes extending from a common superclass. • Consequences: • polymorphism (type hierarachy) • reduced code duplication (inheritance)

  5. Code duplicationa “code smell” package pkg; public class UTA implements TA { private String _name; public UTA(String name) { _name = name; } public String job() { return "teach recitations"; } public String name() { return _name; } } package pkg; public class GTA implements TA { private String _name; public GTA(String name) { _name = name; } public String job() { return "grade labs"; } public String name() { return _name; } }

  6. Refactored code package pkg; public class UTA extends AbstractTA { public UTA(String name) { super(name); } public String job() { return "teach recitations"; } } package pkg; public class GTA extends AbstractTA { public GTA(String name) { super(name); } public String job() { return "grade labs"; } } package pkg; public abstract class AbstractTA { private String _name; public AbstractTA(String name) { _name = name; } public abstract String job(); public String name() { return _name; } }

  7. Flavors of inheritance • Inheritance is used for different purposes: • to indicate conceptual relatedness (is-a) • for subtyping (extension of capabilities) • for code sharing • Inheritance can take place from • interface to interface • class to class

  8. Inheritanceconceptual relatedness (is-a) • The is-a relationship exists between two classes A and B if each member of A is also a B. • Example 1: since every square is also a rectangle, we say square is-a rectangle. • Example 2: since every penguin is also a bird, we say penguin is-a bird.

  9. is-a • can imply a restriction in capabilities

  10. shape polygon parallelogram rhombus rectangle square Exhibit ‘A’ ellipse (conceptual) is-a hierarchy

  11. Inheritancesupertype-subtype • A subtype has at least as many capabilities as the supertypehas: • Java uses the “extends” keyword • The compiler uses typing information to ensure that method calls made on expressions of a given type are coherent. • “extends” implies an augmentation of capabilities

  12. types (continued) • rectangle • vary width and height independently of each other • independent setWidth and setHeightmutators. • square must not allow this • width and height are related • setSizemutator only • square and rectangle are NOT the same type. • rectangle and ellipse might be (!)

  13. width (w) (x,y) height (h) (x+w,y+h) Rectangles & Ellipses width (w) (x,y) height (h) (x+w,y+h) The dimensions of rectangles and ellipses are specified in the same manner.

  14. types (continued) • square is-a rectangle conceptually • square should not inherit from rectangle (rectangle methods are not a subset of square methods)

  15. shape polygon parallelogram rhombus rectangle square Exhibit ‘B’, part 1 ellipse type hierarchy

  16. shape rectangularly-defined shape square Exhibit ‘B’, part 2 ellipse rectangle type hierarchy

  17. lesson • There is no single “right” hierarchy. • Arrange hierarchy so it works for your problem/solution.

  18. Inheritance:sharing implemenation • A third use of inheritance is for sharing of implementation (code). • This use of inheritance is over-used: composition is often preferable. • Advice: • never use inheritance SOLELY for the purpose of sharing implementation • OK if coupled with is-a or typing motivation

  19. shape polygon parallelogram rhombus rectangle square Exhibit ‘C’ ellipse (one way to achieve) code sharing

  20. interface inheritance • In Java, an interface can extend zero or more interfaces: • extending zero interfaces public interface A { public void foo(); } • extending one interface public interface B extends A { public void bar(A a); } • extending many interfaces public interface C extends B, ActionListener { public ActionEventgetEvent(); }

  21. implements and extends clauses • a class can implement an arbitrary number of interfaces • a (user-defined) class extends exactly one class • Object by default

  22. class (implementation) inheritance • A (user-defined) class always extends exactly one (other) class: public class Circle extends Shape {…} • If class header has no explicit extends clause, parent class is implicitly Object: public class Shape {…} public class Shape extends Object {…} • Object is the root of Java’s class hierarchy.

  23. implements clause • a class can implement an arbitrary number of interfaces • type hierarchy vs. class hierarchy • class: single root – Object • type: many roots • all instantiable types fall under Object  all objects are of type Object

  24. interface inheritance,cont’d • When an interface A extends another interface B, A inherits the methods specified by B. • This means that a class which implements A must define all the methods specified in both A and B. • If an interface can have at most one specification for any given method: even if an interface inherits the very same method specification (same name, same parameter list) from two or more parent interfaces, the interface has the method specified just once.

  25. What is inherited? • Given what we know, a correct answer is that anything that is not private is inherited. • All our properties (instance variables) are private, so they are not inherited. • All our methods are public (not private), so they are inherited.

  26. What is effectof inheritance? • A method inherited from a superclass to a subclass can be invoked on a subclass instance, even though not defined there: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } } public class FooSub extends Foo { … } • This is legal: new FooSub().setBar(new Bar())

  27. multiple inheritance • Some languages allow multiple (implementation) inheritance (e.g. C++) • Java does not (but Java has interfaces) • Issue: • if the same method, defined in different ways, is inherited from different ancestors, which implementation has priority?

More Related