Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"— 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 duplication a “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 Inheritance conceptual 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 Exhibit ‘A’ (conceptual) is-a hierarchy shape rectangle square rhombus parallelogram polygon ellipse

11 Inheritance supertype-subtype A subtype has at least as many capabilities as the supertype has: –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 setHeight mutators. square must not allow this –width and height are related –setSize mutator only square and rectangle are NOT the same type. rectangle and ellipse might be (!)

13 Rectangles & Ellipses (x,y) width (w) height (h) (x+w,y+h) (x,y) width (w) 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 Exhibit ‘B’, part 1 type hierarchy shape rectangle square rhombus parallelogram polygon ellipse

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

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 Exhibit ‘C’ shape rectangle square rhombus parallelogram polygon (one way to achieve) code sharing ellipse

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 ActionEvent getEvent(); }

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


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"

Similar presentations


Ads by Google