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