Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Chapter 10: Introduction to Inheritance
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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 prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
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.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Computer Science I Inheritance Professor Evan Korth New York University.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance in the Java programming language J. W. Rider.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Topic 4 Inheritance.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
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 - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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:
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance 2nd Lecture
Inheritance 2nd Lecture
Inheritance.
Inheritance 2nd Lecture
Advanced Inheritance Concepts
CMSC 202 Inheritance II.
Presentation transcript:

Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class. The derived class inherits the fields and methods of the class. The derived class may have additional fields and methods, and may replace the methods that it inherits.

Benefits of Inheritance Inheritance allows the programmer to write simpler, smaller code. If there are two or more classes that are similar (but not exactly the same), all the commonalities can be put into a single base class. The classes are then derived classes of the base class. Only the differences between the derived classes and the base classes need to be coded.

Example: Bank Account Suppose the we have different types of bank accounts:  Checking account  Checking account with interest  Checking with overdraft protection  Savings account  Medical spending account

Example (cont'd) The fields and methods that are common to all accounts can be put into a single base class. This might include:  Balance  Transaction log  Deposit  Withdraw  Check Balance

Example (cont'd) The various account class would be derived from the base class and would inherit the common fields and methods. We could then add additional fields and methods to customize each derived class. For example, a checking account with interest might have an APR field and a method to add interest to the balance.

Overiding Methods In addition to new methods, a derived class can replace the methods of the base class. This is called overriding a method. For example, the savings account might have restriction on the number of withdrawals that can be made in a month. So, the withdraw method would be different for the savings account class.

Syntax To create a derived class, we use the keyword extends in the class heading followed by the base class. For example: public class SavingAcct extends BankAccount { }

Hierarchy of Classes A derived class may also be the base class for another derived class. That is, you can have a chain of derived class. There is a hierarchy of classes, that is, a tree of all classes in Java. The root of the tree, the class from which all other classes are derived is called Object. All classes inherit the methods of Object.

Terminology We often use the term subclass for a derived class and superclass for the base class. Also some use the terms child class and parent class. We also use the terms descendant class and ancestor class for class related by more than one level of inheritance. Note that deriving a class is very different from instantiating a class. We are not creating any objects, but rather, modifying a blueprint.

Overriding Methods A derived class may override a method inherited from the base class. We simply write a new method with the same name and parameter list (number and types – names don't matter here) as the method in the base class. When the method is called upon an object of the derived class, the derived class' version of the method is invoked.

Overloading Methods Note that overriding a method is different from overloading a method. We overload a method when the parameter list of the new method is different (different number or types) than the parameter list of the original method. Java will call the appropriate method based on the number and types of the arguments in the call to the method. It is a common mistake to overload when we me to override a method.

The Return Type In general, you cannot change the return type of an overridden method. The exception is that if the base method has a return type which is a class, the derived class may have a return type which is a descendant of the base class' return type. For example, if the BankAccount class has a method that returns a BankAccount, the SavingsAcct class could override that method and return a SavingsAcct.

Restricting Access Some times we only want to allow methods of a derived class to call a method in the base class. We can use the access modifier protected (as opposed to public or private) to do so. That is, if a method or field is protected, only the methods of the class and any derived classes may access that field or method.

Object Class All classes are descendants of the Object class. The Object class has no fields but does have several useful methods, for example, the toString method. However, these methods work for an generic object, not your specific object and may not do what you want. Thus, it's often useful to override these methods.

toString Method The toString method is used to return (as a String) a human-readable description of the object. It should create a string with the information from the pertinent fields formatted in an easily understandable way. The default toString method prints out the name of the class and a representation of the pointer to the data object.

equals Method The equals method is useful for comparing two objects to check if they are the same. The default equals method returns true if and only if the two objects are the same data object in memory. Usual, you should override this method to return true if and only if the two objects represent the same thing, i.e., the relevant fields are equal.

equals Method (cont'd) Note the definition of the equals method is: public boolean equals(Object other) That is, the argument is of type other. To use the fields and methods of the actual class of objects being compared, you will need to typecast other to the right type, i.e., ((Ship)other).size.

Other Useful Stuff The instanceOf operator checks if an object is of a particular type. The syntax is: instanceof The getClass() method of class Object returns the class that was used to create the object.

Abstract Class An abstract class is a class that can never been instantiated. It should be used for a base class when no generic object will ever be created, but rather, only objects of the derived classes can be created. For example, in the Nethack program, there is no such thing as a generic Creature, only Dragon, UmberHulk, Human, etc. The keyword abstract is used to denote an abstract class.

Using an Abstract Class One time we need to use an abstract class is when we have an array which contains objects of different class. The abstract base class is the type of the object stored in the array. For example, in the Battleship program, the board had a field which was a 10x10 array of Thing, where a Thing could be a Ship, Water, Hit, or Miss. Thing would be an abstract base class.

Abstract Base Class (cont'd) An abstract base class may have fields and methods that are inherited by the derived classes.

Abstract Method On ocassion, all the derived classes have the same method, but the method is different for each derived class. That is, there is no one generic method that is used by any class. However, we would like to specific that every derived class should have such a method (we just don't know what the code is!) An abstract method in a base class is one that has no code, but must be overridden in any derived class.