Class Hierarchies and Type Conformance

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Web Design & Development Lecture 19. Java Graphics 2.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Lecture 10.2 Different Types of Loops, including for and do.
Geometry 5 Level 1. Interior angles in a triangle.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
1 Inheritance and Subclasses Instructor: Mainak Chaudhuri
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Inheritance Like father like son Image from:
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Square rectangle triangle diamond trapezoid pentagon hexagon circle oval heart star.
2-D shapes How do most 2-D shapes get their names? 3 – Triangle © Can you think of some shapes that are just named after.
Creating Your Own Widgets CompSci 230 S Software Construction.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
EVENTS Wiring together objects, code, and actions.
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
2D shapes © D shapes How do most 2D shapes get their names? 3 – Triangle 5 – Pentagon 6 – Hexagon 7- Heptagon 8 – Octagon.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved Designing with Aggregation and Inheritance Software.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Creating Your Own Widgets
Aggregation and Inheritance
How to Design Supplier Classes
public class Doubler extends Base {
More Boolean and Relational Expressions
What hath George Boole wrought?
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Comp 249 Programming Methodology
Chapter 10 Datapath Subsystems.
Chapter 20 Hash Tables.
Shapes.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
The Facts to Be Explained
Chapter 4 Constructors Section 4.4
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Chapter 2 Part 1 Data and Expressions.
Chapter 2 Reference Types.
Presentation transcript:

Class Hierarchies and Type Conformance Lecture 9.1 Class Hierarchies and Type Conformance

Inheritance Systems Merchandise Electronics Clothing Television Camcorder Shirt Dress Shoe Digital Analog © 2006 Pearson Addison-Wesley. All rights reserved

Another Hierarchy AcademicDisciplines Mathematics Engineering Algebra Geometry Industrial Electrical Civil Probability © 2006 Pearson Addison-Wesley. All rights reserved

Ancestors & Descendants ClosedFigure Ellipse Polygon Circle Square Quadrilateral Pentagon Triangle Ancestors Descendants © 2006 Pearson Addison-Wesley. All rights reserved

Conformance ClosedFigure Ellipse Polygon Circle Square Quadrilateral Pentagon Triangle A type conforms to itself or any ancestor. A conforms to B means A is_a B Conformance is directional! © 2006 Pearson Addison-Wesley. All rights reserved

Rule for Using Conformance An expression is permitted anywhere that its type conforms. assignment object = expression; expression must conform to object parameter passage object. method(arg1, arg2, … argN); arg1 must conform to parm1 arg2 must conform to parm2 ... argN must conform to parmN public void method(parm1, parm2,…parmN) { … } expressions expression Anything with type that conforms to expression can be substituted in its place. Remember: Conformance is directional! © 2006 Pearson Addison-Wesley. All rights reserved

javax.swing.JComponent java.awt.Component «queries» + Color getBackground() + Color getForeground() + int getX() + int getY() + int getWidth() + int getHeight() + Container getParent() «update» + void setBackground( Color ) + void setForeground( Color ) + void setBounds(int, int, int, int) + void setSize( int, int ) + void setLocation( int, int ) + void paint( Graphics ) + void repaint() java.awt.Container «update» + void add( Component ) + void add( Component, int ) + void remove( Component ) + void removeAll( ) javax.swing.JComponent «constructor» + JComponent() «queries» + int getX() + int getY() + int getWidth() + int getHeight() «update» + void paint( Graphics ) + void paintChildren() + void setBackground( Color ) + void setForeground( Color ) Component -- note getParent returns the object to which the Component has been added or null. The add method with no second container adds UNDERNEATH existing children. removeAll removes all children. JComponent Inheritance © 2006 Pearson Addison-Wesley. All rights reserved Oval Rectangle Line

Using Inheritance and Conformance Write a function that returns true exactly when its Oval parameter has a height of 10 or greater. public boolean isTenOrMoreHigh( Component v ) { return v.getHeight() >= 10; } What is the advantage of using a parameter of type Component, instead of an Oval? © 2006 Pearson Addison-Wesley. All rights reserved

Testing for Type conformance Java provides the following operator to test for type conformance. instanceof Syntax expression instanceof className Semantics This is a Boolean expression that evaluates to true exactly when the type of expression conforms to className. Example if (myVar instanceof Component) ... © 2006 Pearson Addison-Wesley. All rights reserved