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

Similar documents
CompSci 125 Lecture 07. Chapter 4: Object-Oriented Development

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

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

ECE 122. Engineering Problem Solving with Java

Java Object Oriented Design. CSC207 Fall 2014

What is Inheritance?

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

COMP 110/L Lecture 19. Kyle Dewey

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

CS Programming I: Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Inheritance and Polymorphism

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

ITI Introduction to Computing II

Inheritance. Transitivity

Comp 249 Programming Methodology

ITI Introduction to Computing II

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

INHERITANCE. Spring 2019

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CS 251 Intermediate Programming Inheritance

Object-Oriented Programming

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

Multiple Inheritance, Abstract Classes, Interfaces

CSC207 Week 3. Larry Zhang

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Lecture 18 CSE11 Fall 2013 Inheritance

Overriding Variables: Shadowing

Computer Science II (20073) Week 1: Review and Inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Building custom components IAT351

Inheritance and Polymorphism

Inheritance and Polymorphism. CS180 Fall 2007

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Binghamton University. CS-140 Fall Dynamic Types

Object-Oriented Design

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Computer Science 210: Data Structures

Object-Oriented Design

INHERITANCE AND EXTENDING CLASSES

More on Objects in JAVA TM

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Java Fundamentals (II)

Java: introduction to object-oriented features

Inheritance and Encapsulation. Amit Gupta

C++ Important Questions with Answers

CS1150 Principles of Computer Science Objects and Classes

Programming overview

Object Oriented Programming. Java-Lecture 11 Polymorphism

Declarations and Access Control SCJP tips

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

CMSC 132: Object-Oriented Programming II

Chapter 14 Abstract Classes and Interfaces

CS61B Lecture #13: Packages, Access, Etc.

Software Design and Analysis for Engineers

CS111: PROGRAMMING LANGUAGE II

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

CS111: PROGRAMMING LANGUAGE II

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Arrays Classes & Methods, Inheritance

Basic Object-Oriented Concepts. 5-Oct-17

Chapter 11: Inheritance

Inheritance, Polymorphism, and Interfaces

Practice for Chapter 11

Overview. Elements of Programming Languages. Objects. Self-Reference

Data Structures (list, dictionary, tuples, sets, strings)

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Rules and syntax for inheritance. The boring stuff

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

ECE 122. Engineering Problem Solving with Java

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

ECE 122. Engineering Problem Solving with Java

9/10/2018 Programming Data Structures Inheritance

CSE 401/M501 Compilers

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

PIC 20A GUI with swing

Inheritance -- Introduction

C08: Inheritance and Polymorphism

CS-202 Introduction to Object Oriented Programming

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Inheritance (Part 2) Notes Chapter 6

CS61B Lecture #13: Packages, Access, Etc.

Object Orientated Programming Details COMP360

OVERRIDING. 7/11/2015 Budditha Hettige 82

CH. 2 OBJECT-ORIENTED PROGRAMMING

Lecture 10 Declarations and Scope

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Transcription:

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

Announcements

Introduction to Inheritance

Inheritance: Motivation Software is expensive Wish to define a new class by identifying only how it differs from an existing class Don t want to rewrite the entire existing class

Inheritance extends! Parent class Child inherits from parent Extend functionality Modify functionality Parent Class Child Class

Alternative Terminology Parent class Super class Base class Ancestor class Child class Subclass Derived class Heir class

Inheritance Example public class MyPanel extends JPanel {!!!//Child MyPanel inherits from JPanel parent!! }!

What does Child Inherit? Member Methods Member Variables Member Nested Classes (Not yet discussed in CS125) Not constructors

Thus If child does nothing but inherit, it s a replica of parent sans its constructors (more about this later) Perfectly legal: JComponent p = new JPanel();! Because JPanel inherits from JComponent Because JPanel is a JComponent JPanel implements JComponent s methods JPanel implements JComponent s variables

Child May Differ from Parent Methods Child may define methods not available in the parent Child may override methods defined by the parent Variables Child may define new variables not available in the parent Child may shadow parent s variables (unsafe practice) Constructors Child does not inherit parent s constructors Child may define its own constructors

Chained Inheritance Grandchildren inherit from child Grandchildren also inherit through child from parent Container JComponent JPanel

What About private Members? Child inherits parent s private variables and methods Child has no access to them (cannot reference them) Private members are present in the child but invisible

Parent Knows Nothing About Child Inheritance is a one-way street Parent has no knowledge or access to child s variables nor methods If class MyPanel extends JPanel, then JPanel knows nothing about the variables and methods in MyPanel

Instantiating Child does not Instantiate the Parent Class If your class MyPanel extends JPanel And if you construct ( new-up ) an instance of a MyPanel You built only a MyPanel; you did not also build a JPanel But a MyPanel implements JPanel s methods/variables

Child May add Functionality not Available in the Parent Class public class MyPanel extends JPanel {!!!private ArrayList<Point> mypoints;!!!public void drawmypoints() {!!!.!!!.!!!.!!} //drawpoints!! } //MyPanel!

Child May Override Functionality Defined by its Parent Class public class MyPanel extends JPanel {!!!public void paintcomponent(graphics g) {!!!.!!!.!!!.!!} //paintcomponent!! } //MyPanel!

Bad Practice: Child may even Override Variables public class MyPoint extends Point {!!!public double x, y;!.!!.!!.!! } //MyPoint!!//Shadow variables!

super! Refers to functionality defined by the parent class super.doit();!//invokes parent s doit() method! super();!!//invokes parent s constructor!

Example: Invoking a method defined in a parent class public class MyPanel extends JPanel {!!!public void paintcomponent(graphics g) {!!!super.paintcomponent(g);!!!.!!!.!//draw additional artwork here!!!.!!} //paintcomponent!! } //MyPanel!

Inheritance and Constructors Class members include instance variables and methods Members are inherited and can be overridden Constructors are not members Constructors are not inherited and thus cannot be overridden BUT a child s constructor can invoke a constructor defined in the parent class: super.parentclassname()!

Implicit invocation of super() If your constructor doesn t explicitly invoke a parent s constructor Then Java implicitly inserts: super();! Attempting to ensure the object instance variables defined in the parent class are initialized

Implicit super constructor ParentName() is undefined for default constructor. Must define an explicit constructor super() does not exist The child class must explicitly invoke one of the parent s constructors with parameters Or define a constructor without parameters in parent

Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor super() does not exist Child must explicitly invoke another constructor

UML: Class Diagrams

UML: Class Diagram Overview ClassName Attributes Operations Properties Attributes Associations Operations Methods Constructors

UML has been Rightfully Criticized for its Complexity Toad -x: int! -y: int! -health: int! +count: byte! +move(int,int)! +injure()! +heal()! +gethealth(): int! public class Toad {! //Attributes!! }!! private int x,y;! private int health;! public static byte count=0;! //Operations! public void move(int x, int y) { }! public void injure() { }! public void heal() { }! public static int gethealth() { }!

UML: Class Diagram Dependency Relationship Class1 Properties Operations Class2 Properties Operations Directed, dashed line indicates Class1 depends upon Class2 Transient relationship (as opposed to an association) Class1 invokes methods in Class2 thru a local variable Class1 invokes methods in Class2 thru a parameter

UML: Class Diagram Association Relationship Class1 Properties Operations Class2 Properties Operations Solid, directed line Fixed state relationship (as opposed to a dependency) Class2 appears as the type of an instance variable in Class1 Designer graphically emphasizes the importance of a property

Association or Attribute More or less the same thing! Both usually implemented in Java as instance variables Graphical association emphasizes important, controversial relationships, especially between new/modified classes List attributes suitable for built-in Java, non-controversial, and legacy types that won t change in the design process

UML: Class Diagram Inheritance Relationship AKA Generalization Class2 inherits from Class1 Class1 Properties Operations Class2 Properties Operations

UML Advice UML is important Fowler, M. UML Distilled. Addison Wesley. 2004. Be familiar with these: Class Diagrams Sequence Diagrams Use for design discussions on a whiteboard Capture with camera phone Keep it simple! Because you can doesn t mean you should include it Also: Formal design documentation http://www.shilman.net/cal//research/whysketch.html

Software Design: A Starter Definition What classes are required? How do those classes interact? Use class diagram to explore which classes instantiate another Use class diagram to explore which classes inherit from another When do their objects interact? Use sequence diagram to explore when objects are instantiated Use sequence diagram to explore when objects invoke methods