Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Abstract classes and Interfaces. Abstract classes.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Final and Abstract Classes
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 143 Inheritance.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
More About Inheritance & Interfaces
Final and Abstract Classes
Presentation transcript:

Inheritance (notes for 10/26 lecture)

Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically) an easy relationship to express, but it takes some study to truly understand its many aspects.

Flavors of inheritance Inheritance 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: 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. The picture on the next slide, shows this idea.

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

Inheritance: types Inheritance can also indicate a subtype- supertype relationship. A subtype has at least as many capabilities as the supertype has. The compiler uses typing information to ensure that method calls made on expressions of a given type are coherent.

types (continued) It is possible to imagine that a rectangle object in a drawing program allows its width and height to be varied independently of each other, via independent setWidth and setHeight mutators. A square object must not allow this. A square object in such an environment should only provide a setSize mutator. From this perspective, squares and rectangles are NOT of the same type. A rectangle and an ellipse might be, however.

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.

types (continued) While a square is-a rectangle conceptually, in this environment it does not make sense for the type square be be a subtype of the type rectangle (since the set of methods on a rectangle are not a subset of those of the square).

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

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

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

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

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 ActionListener getListener(); }

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 it 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.

class 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.

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? 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())

Method overriding A subclass can override a definition inherited from superclass simply by providing an alternate definition of the method in the subclass’ defintion: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } public class FooSub extends Foo { public void setBar(Bar b) { _bar = b; // of course, we can’t do this, since _bar is private! b.setColor(new graphics.colors.Cyan()); }

partial overriding A subclass can add something to a definition inherited from superclass simply first invoking the superclass’ definition, then adding extra code in an augmenting definition of the method in the subclass’ defintion: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } public class FooSub extends Foo { public void setBar(Bar b) { super.setBar(b); // asks superclass method to do: _bar = b; b.setColor(new graphics.colors.Cyan()); }

overriding summary total (complete) overriding –a subclass provides an entirely new definition for a method which would otherwise have been inherited from the superclass partial overriding –a subclass provides a definition for a method which would otherwise have been inherited from the superclass, but calls the superclass version via super. inheritance –a subclass does not provide an alternate defintion for a method defined in the superclass, which is inherited.

A few loose ends… The next several slides ties up a few loose ends.

constructor and method overloading A class can define multiple methods with the same name, as long as they differ in their parameter lists. A class can therefore define multiple constructors (which all MUST share their name), as long as they differ in their parameter lists. Providing multiple method definitions with the same name is called overloading: the name is overloaded with multiple definitions. Selection of correct definition is based on the argument list in a given method call.

overloading vs. overriding overloading: –same name is used for many different method definitions –parameter lists of methods must all be different –all definitions co-exist overriding: –same name is used for a subclass method also defined in a superclass –parameter list must be exactly the same in subclass definition as in superclass definition –subclass definition supplants superclass definition

constructor overriding Constructors can (and should generally) be overridden when extended classes other than Object. A superclass constructor is always called, implicitly if not explicitly. Implicit call is to super(). Use super with argument list to explicitly call one of the superclass’ other constructors. Call to superclass constructor is ALWAYS the first statement. If not, compiler complains.

no constructor? A class must always provide a constructor. If not, one is provided implicitly: a no- argument constructor which just calls the implicit superclass constructor.

a minimial class definition In Java we can write: public class Foo { } In reality the compiler sees this: public class Foo extends Object { public Foo() { super(); }

this A pronoun that an object uses to refer to itself. A reference to the object on which a method was invoked.

java.lang.System.out.println method We can output (print) a String by using the method System.out.println System is a class, in the package java.lang, all of whose members are automagically imported (i.e. we don’t need to use their fully-qualified names to access them). out is a variable accessible via this class name (hmm – how does that work?) println is a method defined on the object referred to by the out variable. out is of type java.io.PrintStream, and overloads println in many ways.