What is inheritance? It is the ability to create a new class from an existing class.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
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.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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 and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Java Inheritance.
Inheritance.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

What is inheritance? It is the ability to create a new class from an existing class.

Inheritance  The objectives of this chapter are:  To explore the concept and implications of inheritance  Polymorphism  To define the syntax of inheritance in Java  To understand the class hierarchy of Java  To examine the effect of inheritance on constructors

Terminology  Inheritance is a fundamental Object Oriented concept  All classes can be defined as a sub class of another class  The super class is the more general class (few details)  The sub class is the more specific class (many details) Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date superclass: subclass:

 At the very top of the inheritance hierarchy is a class called Object  All Java classes inherit from Object  Every class without the extends keyword automatically extends the class Object  The Object class is defined in the java.lang package The Object Class

 All methods defined in the class Object are available in every class including  equals(otherObject)  Compares two objects for equality, which is defined in Object as true when two object references point to the same object (x==y)  toString()  Returns a string representation of the object  Both of these methods should be overridden in a child class so they are defined more precisely for that class The Object Class

Types of Inheritance Single Inheritance A child can only have one parent Multiple Inheritance A child can inherit from multiple parents Multiple inheritance is not permitted in Java Single inheritance can be limiting – for example, Person can be a Student or Employee but not both

Super Class and Sub Class  A superclass, parent class, base class is a generalized class  A subclass, child class, derived class is a more specific, detailed version of a superclass

Inheritance in Java Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date public class Person { private String name; private Date dob; [...] public class Employee extends Person { private int employeID; private int salary; private Date startDate; [...] Employee anEmployee = new Employee();

Inheritance in Java  Each class has only one parent  Inheritance creates a class hierarchy  No limit to the number of subclasses a class can have  No limit to the depth of the class hierarchy Class

IS-A Relationship Use the extends keyword to establish an inheritance relationship  Sub extends Super public class Animal { } public class Cat extends Animal { } public class Tiger extends Cat { } Cat is-a Animal Tiger is-a Cat Tiger is-a Animal as well These are “siblings”

Inherited by sub class:  Variables of the super class with public or protected visibility  Methods of the super class with public or protected visibility NOT inherited by sub class:  Constructors of the super class  Variables and methods of the super class with private visibility  Use public accessor and modifier methods to access the private members of the super class What Exactly is Inherited?

public class ClassA { public int x; private int y; public int z; … } public class ClassB extends ClassA { public int a; private int b; … } public class ClassC extends ClassB { private int q; … } What instance data is accessible in ClassA? ClassB? ClassC? x, y, z a, b, x, z q, a, x, z

 A sub class is a more specialized type of the super class  A child class has to provide its own constructor(s)  The first statement in the child class constructor is a call to super() or super(parameter list) to set up the “parent part” of the object  If you don’t call super() explicitly, it will automatically be done anyway  You cannot call the superclass constructor from any other method super( )

 The constructor of the superclass must execute first, then the constructor of the subclass Constructors public class Animal { private String name; public Animal(String name) { this.name = name; } public class Dog extends Animal { public String breed; public Dog(String name, String breed) { super(name); this.breed = breed; } Call to the parent class super(…) must be the first line in the sub class constructor

 Constructors are never inherited!  If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass  If you don’t have a no-arg constructor, but do have a constructor with parameters, a compiler error will occur  Make it a habit to include a no-arg constructor for classes you write to avoid this Constructors and super

public class BankAccount { private String ownersName; private int accountNumber; private float balance; public BankAccount(int anAccountNumber, String aName) { accountNumber = anAccountNumber; ownersName = aName; } [...] } public class OverdraftAccount extends BankAccount { private float overdraftLimit; public OverdraftAccount(int anAccountNumber, String aName, float aLimit) { super(anAccountNumber, aName); overdraftLimit = aLimit; } Constructor Example This is the child class First line of the child class constructor is the call to super

 When an object reference variable of a superclass is declared, it can point to objects of its class or objects of any of its subclasses  Legal declarations: Animal a = new Animal(“Alex”); Animal b = new Dog(“Bo”, “collie”);  You cannot use a subclass reference for an object of the superclass type! Dog d = new Animal(“Spot”);  invalid! Constructing Subclass Objects

 If one class extends another which extends another, you can imagine a whole chain of constructors going all the way back to Object  This is called constructor chaining Constructor Chaining

Class Hierarchy UML notation is used to show relationship between classes Open arrow points up at the super class from a subclass

UML Notation - Class Team - teamName: String - playerList: ArrayList - numPlayers: int + Team + addPlayer: boolean + outputTeamDetails: String Name of class (italics if an abstract class) Attributes of the class -- name : return type Constructor and methods of the class The plus (+) and minus (-) signs indicate if the member is public or private. You may also see a hash (#) sign which indicates the member is protected (private to all but subclasses).

UML Notation - Other Interface relationships: Unfilled arrow, dashed line Association relationship: Solid arrow/line shows 1 Customer may have n Orders > at top

A sub class can …  Use the inherited variables and methods of the super class (public and protected) just as they are  Have its own constructor(s) that invokes the constructor of the superclass, explicitly or implicitly  Add new variables and methods – subs are more detailed, remember!  Override an inherited method from super, perhaps to make it more detailed (but can’t override static methods of super!)  Declare a field with the same name as in the superclass, hiding it  Declare a static method with the same name as in the superclass, hiding it What a Subclass Can Do

Method overriding is different than method overloading  Method overriding – A method in a subclass has the exact same method signature as the method in the super class  Method overloading – multiple methods in the same class have the same name but different method signatures (for example, the println method) Method Overriding vs. Overloading

 Method overriding – A method in a subclass has the exact same method signature as the method in the super class  Methods that should be overridden in subclasses toString() equals(Object other)  Methods that cannot be overridden by a subclass  private methods – not accessible to the child classes  final methods – using the “final” keyword to prevents a subclass from altering a method that is critical to the functioning of the class Overriding Superclass Methods

 The best way to redefine a method is to reuse the parent class’ method and supplement it with the code needed to handle the changes in the subclass  If a subclass overrides a method that is also in the parent class, the subclass version of the method is the version that will be executed Method Overriding

 A polymorphic method is a method that has been overridden in at least one subclass  Polymorphism – the mechanism of selecting the appropriate method for a particular object in a class hierarchy  In Java the correct method is chose by the type of the actual object, not the type of the object reference Polymorphism

 If both the superclass and a subclass both have a method with the same name and method signature, that method is polymorphic  Say the Animal class has a method speak() and so does the Dog class which extends Animal Animal a = new Animal(); a.speak(); //displays “a noise” Animal b = new Dog(); b.speak(); //displays “bark” Polymorphic Method Calls At runtime the correct version of the method is determined