ITEC324 Principle of CS III

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Event Handling.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Anonymous Classes A local class is a class that is defined in a block. This could be a method body, a constructor, a local block, a static initializer.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Chapter 10 Classes Continued
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CSSE501 Object-Oriented Development
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
CPSC 2100 University of Tennessee at Chattanooga – Spring 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 6: Inheritance and.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Inheritance Horstmann ch , 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
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 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Inheritance and Access Control CS 162 (Summer 2009)
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
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
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.
CS Lecture 04 Mice Lynda Thomas
© 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 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 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Events and Event Handling
Sections Inheritance and Abstract Classes
CompSci 230 S Programming Techniques
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
Computer Science 209 Graphics and GUIs.
Inheritance and Polymorphism
One class is an extension of another.
Inheritance and Abstract Classes
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
One class is an extension of another.
Advanced Java Programming
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

ITEC324 Principle of CS III Chapter 6 (Horstmann’s Book) Inheritance :Adapter Class, Abstract Class Hwajung Lee

Inheritance Hierarchies Modeling Specialization and Generalization 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 When designing systems, look for instances of generalization and specialization

Example Hierarchy of Employee Classes

The Substitution Principle (1) Formulated by Barbara Liskov You can use a subclass object whenever a superclass object is expected example: Employee e; e = new Manager(“Barnie Smith”); ... System.out.println("salary=" + e.getSalary()); Can set e to Manager reference Polymorphism: Correct getSalary method is invoked

The Substitution Principle (2) In Java, the type rules always allow a subclass object to be used when a superclass object is expected. However, the question is whether the subclass object can replace the superclass one, conceptually! Don't use inheritance if substitution principle is violated

Invoking Superclass Constructors Use super keyword in subclass constructor: public Manager(String aName) {    super(aName); // calls superclass constructor    bonus = 0; } Call to super must be first statement in subclass constructor If a subclass constructor does not call a superclass constructor, then the superclass constructor with no parameters is called automatically.

Hierarchy of Swing Components (1) Base of hierarchy: Component Most important subclass: Container

Hierarchy of Swing Components (2)

Graphic Programming with Inheritance Chapter 4: Create drawings by implementing Icon interface type Now: Form subclass of JComponent public class MyComponent extends JComponent {    public void paintComponent(Graphics g)    {       drawing instructions go here    }    ... } Advantage: Inherit behavior from JComponent Example: Can attach mouse listener to JComponent

Graphic Programming with Inheritance Overriding paintComponent (1) Draw a car: public class CarComponent extends JComponent {    public void paintComponent(Graphics g)    {       Graphics2D g2 = (Graphics2D)g;       car.draw(g2);    }    ... private CarShape car; }

Mouse Listeners To Complete the car drawing program (1) Attach mouse listener to component Can listen to mouse events (clicks) or mouse motion events public interface MouseListener { void mouseClicked(MouseEvent event); void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); }

Mouse Listeners To Complete the car drawing program (2) public interface MouseMotionListener { void mouseMoved(MouseEvent event); void mouseDragged(MouseEvent event); }

Mouse Adapter To Complete the car drawing program (3) What if you just want to listen to mousePressed? Listener interface types with many methods have corresponding adapter classes with do-nothing methods. Extend the adapter rather than implementing the listener.

Mouse Adapter To Complete the car drawing program (4) public class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }

Mouse Adapter To Complete the car drawing program (5) Extend MouseAdapter Component constructor adds listener: addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { mouse action goes here } });

Car Drawing Program ch6/car/CarShape.java ch6/car/CarComponent.java ch6/car/CarMover.java

Abstract Classes (1) An abstract method is undefined and must be defined in a subclass. A class with one or more abstract class methods must be declared as an abstract class. public abstract class SelectableShape implements SceneShape { … }

Abstract Classes (2) You can not construct an object of an abstract class. SelectableShape shape = new SelectableShape(); //Error SelectableShape shape = new HouseShape(); //OK

Abstract Classes (3) SceneShape.java SelectableShape.java HouseShape.java CarShape.java ScenePanel.java SceneEditor.java