1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
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 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Event Handling Events and Listeners Timers and Animation.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
CPSC 2100 University of Tennessee at Chattanooga – Spring 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 6: Inheritance and.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Chapter 5 Introduction to Defining Classes
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
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 Fifth Edition Chapter 9 Introduction to Inheritance.
Lecture 12 Inheritance.
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Inheritance and Polymorphism
One class is an extension of another.
An Introduction to Inheritance
One class is an extension of another.
ITEC324 Principle of CS III
Java – Inheritance.
Inheritance.
Inheritance and Polymorphism
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

1 Inheritance

2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class that inherits is called a child class or subclass inherited from parent classsuperclass –the class that is inherited from is called a parent class or superclass

3 Characteristics of Inheritance Child classes are both extensions and contractions of parent classes: –extensions because they can add characteristics beyond those inherited, as well as override inherited characteristics –contractions because they are more specialized, and thus more restricted than parent class Inheritance is always transitive: a class can inherit features from a superclass that has inherited from another superclass, etc.

4 Forms of Inheritance: Specification Inheritance used to guarantee that classes share a common interface Subclass is realization of incomplete abstract specification, not refinement of existing type Mechanisms for this type of inheritance include interfaces and abstract classes and methods

5 Forms of Inheritance: Specialization Most common use of inheritance New class is specialized variety of parent Satisfies specifications of parent, but extends capabilities: overridden –One or more inherited methods may be overridden –New fields/methods may be introduced

6 Inheritance in Java extendsSubclass denoted by keyword extends Subclass declaration only contains differences from superclass –additional fields/methods –overridden methods

7 Inheritance hierarchies Real world: Hierarchies describe general/specific relationships –General concept at root of tree –More specific concepts are children Programming: Inheritance hierarchy –General superclass at root of tree –More specific subclasses are children

8 Designing class hierarchy Collect common properties in superclasses at base of hierarchy (root) Further down the tree get more specialization; classes at leaves are most specialized

9 Substitutability Idea that type given in variable declaration does not have to match type associated with value variable is holding Can occur through inheritance: for example, variable of superclass type can hold subclass objects Can also occur through use of interfaces - specifically with parameter passing

10 Substitutability Liskov substitution principle: since subclass inherits superclass behavior, can substitute subclass object when superclass object expected Polymorphism: client calls method on object (perceived to be) of parent type; if child object used instead, overriding method is executed

11 Invoking superclass methods superCan call inherited (parent) method from overriding (child) method using keyword super: public void aMethod() { super.aMethod(); // calls parent version }

12 Notes on use of super Without keyword, method call in previous example would be recursive super is not a variable - doesn’t hold a reference to a superclass object - more like invoking a static method Turns off polymorphic call mechanism, frees superclass method to be called

13 Invoking superclass constructors  Use super keyword in subclass constructor: public Manager(String aName) { super(aName); // calls superclass constructor bonus = 0; }  Here, super is used as name of method

14 Invoking superclass constructors  Call to super must be first statement in subclass constructor  If subclass constructor doesn't call super, superclass must have constructor without parameters

15 Preconditions & inherited methods Subclass method cannot require stronger precondition than method it overrides If superclass method had no precondition, subclass method can’t require one

16 Postconditions & inherited methods Subclass method: –postcondition must be at least as strong as postcondition of original –cannot be more private than original –cannot throw more more checked exceptions than original

17 Graphics programming with inheritance To draw shapes, can implement Icon interface, as we have seen More versatile method: subclass Jpanel, override paintComponent() method Advantage: inherit rich behavior set –For example, can attach mouse listener to panel –With interface, you start from scratch; with inheritance, you get readymade features of superclass

18 Overriding paintComponent() Example - draw a car: public class MyPanel extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; car.draw(g2); }... }

19 Problem: corrupted panel Screen corrupted when moving pane Solution: call super.paintComponent(g) as first line in overriding method to refresh panel each time image is redrawn

20 Java Event Model Event: an action, e.g. mouse click, key press, menu selection Listener: object whose purpose is to wait for an event to occur, and perform appropriate action when event is detected Buttons & scrollbars are examples of objects that must incorporate listeners

21 Mouse Listeners There are 5 different mouse-related events, so the MouseListener interface accounts for all: public interface MouseListener { public void mouseClicked (MouseEvent e); public void mouseEntered (MouseEvent e); public void mouseExited (MouseEvent e); public void mousePressed (MouseEvent e); public void mouseReleased (MouseEvent e); }

22 Mouse Listeners If a class implements this interface, it must provide definitions form all of these operations (whether you need them or not) If all operations aren’t needed, can use class MouseAdaptor instead –Implements MouseListener methods as empty method bodies –Can inherit from MouseAdaptor, then just override the methods you actually want to use

23 Mouse Listeners Alternatively, you can implement the MouseListener interface and create your own empty methods for operations you don’t need - for example: public void mouseExited(MouseEvent e) {} Method addMouseListener is inherited from Frame - takes a MouseListener instance as its object

24 Using anonymous class to add mouse listener addMouseListener(new MouseAdapter(){ public void mousePressed (MouseEvent event) { mouse action goes here } });