Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Similar documents
PIC 20A GUI with swing

Chapter 10 Classes Continued. Fundamentals of Java

Java Object Oriented Design. CSC207 Fall 2014

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

OVERRIDING. 7/11/2015 Budditha Hettige 82

C++ Important Questions with Answers

CS11 Java. Fall Lecture 3

Inheritance and Polymorphism

INHERITANCE AND EXTENDING CLASSES

Inheritance. Transitivity

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Chapter 5 Object-Oriented Programming

Inheritance, Polymorphism, and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance -- Introduction

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

Lecture 18 CSE11 Fall 2013 Inheritance

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Chapter 14 Abstract Classes and Interfaces

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

ECE 122. Engineering Problem Solving with Java

Event Driven Programming

Chapter 6 Introduction to Defining Classes

Arrays Classes & Methods, Inheritance

2. (True/False) All methods in an interface must be declared public.

CS 251 Intermediate Programming Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

JAVA MOCK TEST JAVA MOCK TEST II

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance and Interfaces

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Inheritance (Part 5) Odds and ends

Lab 10: Inheritance (I)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

What is Inheritance?

Graphic Interface Programming II Using Swing and OOP more advanced issues. Uppsala universitet

Programming II (CS300)

Inheritance, and Polymorphism.

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Introduction to Object-Oriented Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

The AWT Event Model 9

More Relationships Between Classes

UI Software Organization

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSIS 10A Assignment 14 SOLUTIONS

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Unit 7: Event driven programming

AP Computer Science Unit 13. Still More Graphics and Animation.

CIS 190: C/C++ Programming. Lecture 11 Polymorphism

Programming overview

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS111: PROGRAMMING LANGUAGE II

Object Oriented Issues in VDM++

CMSC 132: Object-Oriented Programming II

AP CS Unit 12: Drawing and Mouse Events

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Practice for Chapter 11

PESIT Bangalore South Campus

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Lecture 5: Java Graphics

8. Polymorphism and Inheritance

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

INHERITANCE. Spring 2019

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Programming II (CS300)

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inheritance. COMP Week 12

OOP Subtyping. Lecture 20

More on Objects in JAVA TM

COMPSCI 230. Software Design and Construction. Swing

COMP 110/L Lecture 19. Kyle Dewey

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

Introduction to Object-Oriented Programming

Inheritance and Substitution (Budd chapter 8, 10)

Inheritance and object compatibility

CS313D: ADVANCED PROGRAMMING LANGUAGE

Transcription:

Inheritance 1

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

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. 3

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 4

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

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

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 7

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 8

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 9

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 10

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

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 12

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 13

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 14

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 15

Postconditions & inherited Subclass method: methods 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 16

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 17

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

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 19

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 20

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); 21

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 22

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 23

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