cse115 introduction to computer science i n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSE115: Introduction to Computer Science I PowerPoint Presentation
Download Presentation
CSE115: Introduction to Computer Science I

Loading in 2 Seconds...

play fullscreen
1 / 27

CSE115: Introduction to Computer Science I - PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on

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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CSE115: Introduction to Computer Science I' - ethel


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
Announcements
  • We will return exams some time next week.
agenda
Agenda
  • Today:
    • Inheritance
      • uses of inheritance
      • overuse of inheritance
  • Coming up:
    • Inheritance
      • overriding of methods (full, partial, none)
    • Primitives
    • More control structures
last time
Last time
  • We saw two classes extending from a common superclass.
  • Consequences:
    • polymorphism (type hierarachy)
    • reduced code duplication (inheritance)
code duplication a code smell
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;

}

}

refactored code
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;

}

}

flavors of inheritance
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
inheritance conceptual relatedness is a
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.
slide9
is-a
  • can imply a restriction in capabilities
exhibit a

shape

polygon

parallelogram

rhombus

rectangle

square

Exhibit ‘A’

ellipse

(conceptual) is-a hierarchy

inheritance supertype subtype
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
types continued
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 (!)
rectangles ellipses

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.

types continued1
types (continued)
  • square is-a rectangle conceptually
  • square should not inherit from rectangle (rectangle methods are not a subset of square methods)
exhibit b part 1

shape

polygon

parallelogram

rhombus

rectangle

square

Exhibit ‘B’, part 1

ellipse

type hierarchy

exhibit b part 2

shape

rectangularly-defined

shape

square

Exhibit ‘B’, part 2

ellipse

rectangle

type hierarchy

lesson
lesson
  • There is no single “right” hierarchy.
  • Arrange hierarchy so it works for your problem/solution.
inheritance sharing implemenation
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
exhibit c

shape

polygon

parallelogram

rhombus

rectangle

square

Exhibit ‘C’

ellipse

(one way to achieve) code sharing

interface inheritance
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();

}

implements and extends clauses
implements and extends clauses
  • a class can implement an arbitrary number of interfaces
  • a (user-defined) class extends exactly one class
    • Object by default
class implementation inheritance
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.
implements clause
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
interface inheritance cont d
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.
what is inherited
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.
what is effect of inheritance
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())

multiple inheritance
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?