Download presentation
Presentation is loading. Please wait.
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 Exam 3 on Wednesday 11/10 –covers material from last exam up to and including today’s material Exam review in lecture on Monday 11/08 –study over the weekend, and bring questions to class on Monday Sample questions on web-site over the weekend
3
Agenda Inheritance introduction –quick review from last class Inheritance details –object construction –super –constructor chaining
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 noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; }
6
Refactored code package inheritance; public class Cat extends Noisy { public Cat(String n) { super(n); } @Override public String sound() { return "meow"; } package inheritance; public class Dog extends Noisy { public Dog(String n) { super(n); } @Override public String sound() { return ”ruff"; } package inheritance; public abstract class Noisy { private String _myName; public Noisy(String name) { _myName = name; } public abstract String sound(); public String getName() { return _myName; }
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
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
10
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 (!)
11
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.
12
types (continued) square is-a rectangle conceptually square should not inherit from rectangle (rectangle methods are not a subset of square methods)
13
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
14
Is-a hierarchy (conceptual) is-a hierarchy shape rectangle square rhombus parallelogram polygon ellipse
15
subtyping: extension of capabilities type hierarchy shape rectangle square rhombus parallelogram polygon ellipse
16
subtyping: extension of capabilities type hierarchy shape rectangle square ellipse rectangularly-defined shape
17
Possible code sharing shape rectangle square rhombus parallelogram polygon (one way to achieve) code sharing ellipse
18
lesson There is no single “right” hierarchy. Arrange hierarchy so it works for your problem/solution.
19
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(); }
20
implements and extends clauses a class can implement an arbitrary number of interfaces a (user-defined) class extends exactly one class –Object by default
21
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.
22
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
23
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.
24
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.
25
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())
26
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.