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,

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Class Hierarchy (Inheritance)
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
03/10/14 Chapter 9 Inheritance.
An Introduction to Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
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.
Overriding Methods & Class Hierarchies
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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, change what methods do, etc. Allows reuse  Can extend a class in several ways for different purposes  … based on the same original code

3 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class

4 Inheritance Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Vehicle Car Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent

Example In the Blackjack design example, we had a Player class and a Dealer class  similar classes  each has a hand, takes turns,… We could define a parent class Person that can then be extended to create appropriate subclasses

Inheriting Suppose we have a simple class A class A { private int count; public int method() { return 1; } And we want to define an extended class B with another method…

Inheriting Now B can inherit A and add a new method: class B extends A { public int newMethod() { return 2; } B now has everything from the definitions of both A and B

Inheriting Using A and B: A myA = new A(); B myB = new B(); System.out.println(myA.method()); System.out.println(myB.method()); System.out.println(myB.newMethod()); Output: 112 An instance of B also has all members from A

Parents and Privacy Subclasses cannot access private members class B extends A {…. public void setCount(int count) { this.count = count; } ERROR: count is private in A… this is not A  Just like any other code – can’t access private members We don’t want count to be public…

The protected Modifier Encapsulation says that members should not be public… But we want access to inherited members New visibibility modifer: protected

The protected Modifier A protected variable can be accessed from all code in the same package It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility Key point: Protected variables can be accessed from all subclasses of a given class In UML, use symbol # to mean protected

Example from Text Book # pages : int + pageMessage() : void Dictionary - definitions : int + definitionMessage() : void Words + main (args : String[]) : void

Constructors Constructors are not inherited, but can be added in the usual way: class A {… public A() { count = 1; } class B {… public B(int n) { setCount(n); }

The super Reference Sometimes, we want to access something in the super class For this, we use the super identifier Similar to this, the meaning of super depends on the context Using super gives access to the methods of the super class  This is particularly useful in constructors

The super Reference A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor  Why? To make sure the “parent-part” is initialized This happens whether you like it or not…  By default, super() is called automatically

Constructors (again) class A {… public A() { count = 1; } class B {… public B(int n) { setCount(n); }

Constructors (a more useful example) class A {… public A(int n) { count = n; } class B {… public B() { super(1); }

18 Overriding Methods A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body The type of the object executing the method determines which version of the method is invoked

Overriding Example class Bird { boolean flying; … void flapWings() { flying = true; }

Overriding Example class Penguin extends Bird {… void flapWings() { return; }

Accessing Parent Methods Sometimes you may want to access the definition of a method from the parent class You can use super to do this public void method(int x) { // can put extra stuff here super.method(x); }

22 Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types

Single Inheritance Java supports single inheritance, meaning that a derived class can have only one parent class Java does not support multiple inheritance  e.g. a motorhome is a vehicle and a house We use interfaces to fake multiple inheritance

The Object Class Single inheritance means that we can think of all classes as a single family tree The built-in class Object is the root of this tree  A class that doesn’t explicitly extend anything has Object as its parent class  i.e. these are equivalent class Foo{…} & class Foo extends Object{…} Object contains some methods ( toString, equals ) that are used unless overridden

Built-in Classes It’s also possible to inherit from built-in classes  Even if you don’t know the original implementation, can still add new methods  e.g. list with extra data (ArrayList with additional instance variables); Scanner than can also read in a custom type; ….

When to Inherit Inheritance is a powerful tool in OO design  Easy reuse of code; works naturally with problems that have a hierarchy of objects Don’t confuse inheritance with aggregation  A should extend B only if “A is-a B” or “A is a more specific version of B” or “A can be subsituted for B”  … not just because you need to use the parts of B

Example: Shapes Suppose we want to represent a collection of shapes (in a drawing program)  square, rectangle, circle (maybe others later) All of these have some things in common  position: the x-y coordinates of the shape  getters and setters for the position  translate(x,y): move the shape by this much Create a class Shape that implements these

Example: Shapes Each of these will inherit Shape Rectangle  add instance variables height and width Square  inherit Rectangle and ensure height==width Circle  add radius, interpret position as the centre

Example: Shapes The class definitions:  class Shape {…}  class Rectangle extends Shape {…}  class Square extends Rectangle {…}  class Circle extends Shape {…} Note… this is not right:  class Rectangle extends Square {…} A square is a certain kind of rectangle, and this should be evident in your code

Example: Shapes (partial) UML

Abstract Classes Not every class can be instantiated  use reserved word abstract to indicate that a class cannot be instantiated  used for place-holders in hierarchy  make contain “abstract methods”  e.g. Shape Abstract classes are just for code reuse, and for designing a nice class hierarchy

Using final Methods may be marked final  public final myMethod(){…}  cannot be overridden  forces a method to have fixed behaviour Classes may be marked final  public final class MyClass {…}  cannot be inherited  forces all methods to have fixed behaviour