Class Inheritance Part I

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
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.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Oriented Programming: Inheritance Chapter 9.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
A First Book of C++ Chapter 12 Extending Your Classes.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance.
OOP: Encapsulation &Abstraction
Interface, Subclass, and Abstract Class Review
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Object Oriented Programming: Inheritance
Advanced Java Programming
Computer Programming with JAVA
Java – Inheritance.
Classes and Objects Part 2 Static Class Members and Arrays of Objects
Java Programming, Second Edition
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Class Inheritance Part I Corresponds with Liang Chapter 10

What is Inheritance? Superclasses and subclasses The subclass inherits the members of the base class All instance member variables for the superclass will be present in an instance of the subclass All instance methods of the superclass can be called with respect to an instance of the subclass

Implementing Inheritance in Java The extends keyword specifies the superclass for a class If class1 extends class2, then class1 will be a subclass of class2, so class1 will inherit all the members of class2. The this keyword Refers to the current class The super keyword Refers to the superclass Can use it to call the superclass constructor Can use it to access a superclass method when the subclass overrides this method Accessing superclass members The statements in the subclass methods can access any non-private (i.e. public or protected) members of the superclass.

Superclasses and Subclasses There are similar examples in chapter 10

A Cylinder1 class as a subclass of Circle Extends specifies that Cylindar1’s superclass is Circle The superclass The subclass

A Cylinder1 class as a subclass of Circle Here, the super keyword is used to invoke the superclass’s constructor. The subclass The superclass

A Cylinder1 class as a subclass of Circle Here, the subclass method is calling the inherited method of the superclass. The superclass The subclass

A Cylinder1 class as a subclass of Circle If a local variable or parameter has the same name as a member variable, use the this keyword to resolve the ambiguity. The superclass The subclass

A Cylinder1 class as a subclass of Circle Invoke constructor An application using the Cylinder1 class

A Cylinder1 class as a subclass of Circle Calling subclass methods An application using the Cylinder class

A Cylinder1 class as a subclass of Circle Calling inherited superclass methods An application using the Cylinder class

A Cylinder1 instance is created on the heap…Note that the instance has the Cylinder1 class’s length member variable AND it inherits the Circle class’s radius member variable. main’s frame Cylinder1 object args radius myCylinder length Frame Stack Heap

Frame Stack Heap Cylinder1 frame this radius 5.0 length 2.0 constructor frame this radius 5.0 length 2.0 Cylinder1’s constructor is invoked with respect to the newly created instance. main’s frame Cylinder1 object args radius myCylinder length Frame Stack Heap

Frame Stack Heap 5.0 this Cylinder1 frame this radius 5.0 length 2.0 Circle constructor frame newRadius 5.0 this Cylinder1 constructor frame this radius 5.0 length 2.0 Circle’s constructor is invoked with respect to the newly created instance, and executes. main’s frame Cylinder1 object args radius 5.0 myCylinder length Frame Stack Heap

Cylinder1’s constructor executes after Circle’s constructor terminates. frame this radius 5.0 length 2.0 main’s frame Cylinder1 object args radius 5.0 myCylinder length 2.0 Frame Stack Heap

After constructors terminate, the results from new are assigned into myCylinder. main’s frame Cylinder1 object args radius 5.0 myCylinder length 2.0 Frame Stack Heap

Frame Stack Heap Later in the program…… Circle frame this Cylinder1 findVolume frame this Circle findArea frame this main’s frame Cylinder object args radius 5.0 myCylinder length 2.0 Frame Stack Heap

Advantages of Inheritance No need to “reinvent the wheel” Can make use of pre-existing data and functionality In previous example: radius member variable was inherited code for initializing radius (in constructor) was inherited code for calculating area was inherited Can “specialize” the inherited data and functionality length member variable was created for subclass code for initializing length (in subclass constructor) was created code for calculating volume was created (making use of inherited area calculation)

Overriding Methods A subclass can override the methods of its superclass Overriding is not the same as overloading! Overriding = same signature as superclass method, with different method body and declared at the subclass. If you want specialized behavior at the subclass, override the superclass method. If you don’t want specialized behavior at the subclass, simply use the inherited superclass method.

A Cylinder class that overrides the Circleclass’s findArea method Here, the subclass has its own findArea method…this overrides the findArea method of the superclass.

A Cylinder class that overrides the Circleclass’s findArea method Can still make use of the superclass’s version of the overridden method through use of the super keyword..

Unified Modeling Language Static class diagram: Boxes for representing classes Top part of box contains name Middle part contains attributes Lower part contains operations Solid line with empty arrow describes inheritance.

UML Diagram for Cylinder Inheritance relationship is shown via a line with an triangular arrow from the subclass to the superclass. Classes Attributes Operations Visio drawing of UML diagram

If the findArea method is called on an instance of Cylinder…… Cylinder’s findArea frame this Circle’s findArea frame this main’s frame Cylinder object args radius 5.0 myCylinder length 2.0 Frame Stack Heap

In-Class Group Exercise Create a subclass of Circle called Sphere. This class should inherit everything from Circle, but should override the findArea method and should include its own findVolume method. The only difference between a Sphere and a Circle is the calculations of its dimensions: The area of a sphere is: 4 * PI * r2 The volume of a sphere is: 4/3 * PI * r3 Draw a UML diagram, and the write the code for the class.