Java Programming Lecture 7

Similar documents
Java Programming Lecture 6

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Java Programming. Events and Listeners

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

CPSC 427: Object-Oriented Programming

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

CS111: PROGRAMMING LANGUAGE II

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Chapter 2a Class Relationships

CS1004: Intro to CS in Java, Spring 2005

CS 11 java track: lecture 3

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CS111: PROGRAMMING LANGUAGE II

Chapter 14 Abstract Classes and Interfaces

INHERITANCE. Spring 2019

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Programming in C# Inheritance and Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Polymorphism

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS 251 Intermediate Programming Inheritance

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

Review sheet for Final Exam (List of objectives for this course)

ITI Introduction to Computing II

Java How to Program, 8/e

Super-Classes and sub-classes

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CS112 Lecture: Inheritance and Polymorphism

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Object Oriented Programming. Java-Lecture 11 Polymorphism

ITI Introduction to Computing II

Programming II (CS300)

Polymorphism and Interfaces. CGS 3416 Spring 2018

CST141 Thinking in Objects Page 1

Introduction to Object-Oriented Programming

CSC9T4: Object Modelling, principles of OO design and implementation

More on Objects in JAVA TM

Programming II (CS300)

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Everything is an object. Almost, but all objects are of type Object!

CS 112 Programming 2. Lecture 14. Event-Driven Programming & Animations (1) Chapter 15 Event-Driven Programming and Animations

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance. Chapter 7. Chapter 7 1

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

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

QUESTIONS FOR AVERAGE BLOOMERS

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

CST141--Inheritance 2

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Inheritance -- Introduction

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

BBM 102 Introduction to Programming II Spring Inheritance

CS200: Advanced OO in Java

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS260 Intro to Java & Android 03.Java Language Basics

1.00 Lecture 13. Inheritance

1. (5 points) In your own words, describe what an instance is.

Object-Oriented Programming Concepts

JAVA MOCK TEST JAVA MOCK TEST II

Introduction to Java. Handout-1d. cs402 - Spring

Name: CS 159 Practice Final Fall 2015

Java Programming Hello FX

C++ Important Questions with Answers

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

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

Design Pattern and Software Architecture: IV. Design Pattern

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Name: CS 159 Practice Final Fall 2015

Exam Duration: 2hrs and 30min Software Design

Chapter 5 Object-Oriented Programming

Lab 10: Inheritance (I)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSCE3193: Programming Paradigms

ENCAPSULATION AND POLYMORPHISM

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

CMSC 132: Object-Oriented Programming II

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Chapter 7. Inheritance

Object Orientated Analysis and Design. Benjamin Kenwright

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

COMP 110/L Lecture 19. Kyle Dewey

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

8. Polymorphism and Inheritance

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Transcription:

Java Programming Lecture 7 Alice E. Fischer Feb 16, 2015 Java Programming - L7... 1/16

Class Derivation Interfaces Examples Java Programming - L7... 2/16

Purpose of Derivation Class derivation is used in Java and other OO languages for several purposes: Hooking into the power of a pre-existing system like Java FX. Polymorphic data: A basic data class, with variations. We will use polymorphic types in the Payroll program. Reusable code: We will see this when we get to adapter classes. The existing class is almost what you want, but not quite. Java Programming - L7... 3/16

What is Derivation? A new class is defined by saying what it has or does that is slightly different from an existing class. The keyword is extends The existing class is the base class, or superclass. The new class is the derived class or subclass. A subclass object has ALL the properties of the original class, plus those it defines. Even though the subclass object HAS those members, private members of the base class are not visible to it. When you create a class that is intended as a base class, you often declare some of its members to be protected. Protected members are visible to derived classes but are otherwise like private members. Java Programming - L7... 4/16

Derivation Example Suppose you are writing a payroll program for a company with three kinds of employees: Salaried, Hourly, and Contractor. Employee would be the base class, with these members, inherited by all subclasses: Name, payrate, and other basic info. A constructor and tostring function. A pay() method. that does basic operations common to all employees. The Salaried class would extend Employee and add one member: A pay() method. that uses the right formula for salaried employees. It might call the inherited pay() method. An hourly employee would have additional members to deal with overtime, and another method for tostring(). Java Programming - L7... 5/16

Derived Classes Inherit Members This is a polymorphic data class. base class derived classes Employee name : String payrate : float Employee Salaried name payrate name payrate Salaried Hourly hoursworked : float Hourly name payrate hoursworked Contractor Contractor name payrate Java Programming - L7... 6/16

Polymorphic Functions In our example, the base class and the salaried and hourly classes all have pay() methods. The methods in the derived classes override the method in the base class. If you call pay() for a salaried worker, then Salaried.pay() will be executed. Similarly, Hourly.pay() will be run for Hourly workers. The Contractor class does not have its own pay() function. So if you call pay() for a Contractor, the method inherited from Employee gets executed. The rule is: the most specific applicable method for a polymorphic function is called. If a class does not have a method for the function, the compiler walks up the derivation tree until it finds one. Java Programming - L7... 7/16

Using a Pre-existing System Class derivation is used in Java to allow users to use FX without understanding it or being able to create it. We start by extending Application to produce a window. We then add scenes with panes inside them. Widgets and artwork go inside the panes. Everything we use in a FX window is either An instance of a predefined Java class. An instance of a class derived from a predefined Java class. When we make window applications, we are always dealing with something we can t see and don t fully understand. We do it by learning a few simple rules! Java Programming - L7... 8/16

Deriving from Application FX ActionEvent Application + void start(); EventHandler<ActionEvent> Interface Interacts with ButtonDemo + void start Interacts with RandomColor Java Programming - L7... 9/16

Why do we need Derivation? To allow relatively untrained people to do sophisticated things. To integrate our own code with the predefined classes. To adapt existing code to our own needs. Java Programming - L7... 10/16

What is an Interface? An interface is like a class with functions, but no data members. It consists of a list of abstract functions (function prototype without bodies). To use an interface, one implements it. A class can implement zero or more interfaces. Although interfaces are important in many contexts, they are most heavily used to handle events in GUI programs. Java Programming - L7... 11/16

Implementing an Interface class myclass implements EventHandler <ActionEvent> {... } This declaration says that myclass must implement all of the abstract functions that are defined by the EventHandler<ActionEvent> interface. EventHandler<ActionEvent> actually defines only one function, handle(). So in myclass, we must define a method for handle, Inside the handle method, write code to do whatever is needed, for this program, for an action event. This method will be called when the action event happens. Java Programming - L7... 12/16

Registering an Event Handler Events happen when you click buttons or use other kinds of GUI controls. To connect a specific button to its appropriate handler, you must instantiate a handler class and register it with the button: bt1.setonaction( new myclass() ); Java Programming - L7... 13/16

Why do we need Interfaces? FX is a complicated and sophisticated system that must interact with our own code. FX understands how to make GUI controls generate events and how to catch those events. FX does not understand what to do when each event is caught. An interface class creates a hook that can be used to connect FX with programmer-defined event handlers. Your code overrides the abstract function with a real method. Inside FX, the event code calls the abstract handler function, but the call is passed on to your method, which overrides the abstract method. Java Programming - L7... 14/16

Examples Family: A Swing program that illustrates class derivation Winter and Spring: FX programs that illustrate deriving from Application. CircleDemo: an FX program with shapes. Java Programming - L7... 15/16

Homework This Week Read Chapter 14: Java FX Basics Java Programming - L7... 16/16