Reusing Classes in Java. Java - Inheritance/Polymorphism/Interface. Simple Example of Composition. Inheritance. Composition.



Similar documents
Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

1. Polymorphism in C++...2

Abstract Class & Java Interface

Java Interview Questions and Answers

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

CS 111 Classes I 1. Software Organization View to this point:

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Java Programming Language

Description of Class Mutation Mutation Operators for Java

History OOP languages Year Language 1967 Simula Smalltalk

AP Computer Science Java Subset

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 13 - Inheritance

Java CPD (I) Frans Coenen Department of Computer Science

Moving from CS 61A Scheme to CS 61B Java

Fundamentals of Java Programming

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

Part I. Multiple Choice Questions (2 points each):

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Inheritance, overloading and overriding

Java Application Developer Certificate Program Competencies

The Interface Concept

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

13 Classes & Objects with Constructors/Destructors

C++ INTERVIEW QUESTIONS

Advanced Data Structures

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

java Features Version April 19, 2013 by Thorsten Kracht

Using Inheritance and Polymorphism

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

D06 PROGRAMMING with JAVA

Compile-time type versus run-time type. Consider the parameter to this function:

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Example of a Java program

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Chapter 1 Fundamentals of Java Programming

Contents. 9-1 Copyright (c) N. Afshartous

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

J a v a Quiz (Unit 3, Test 0 Practice)

CS506 Web Design and Development Solved Online Quiz No. 01

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

CS170 Lab 11 Abstract Data Types & Objects

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Glossary of Object Oriented Terms

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Introduction to Data Structures

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Java (12 Weeks) Introduction to Java Programming Language

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Web Development in Java

Object Oriented Software Design

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Basic Object-Oriented Programming in Java

Lecture 7: Class design for security

Adapter, Bridge, and Façade

An Introduction To UML Class Diagrams Classes

Programmation 2. Introduction à la programmation Java

Collections and iterators

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

Lab Manual: Using Rational Rose

Part 3: GridWorld Classes and Interfaces

C++FA 5.1 PRACTICE MID-TERM EXAM

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

11 November

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS193j, Stanford Handout #10 OOP 3

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Construction of classes with classes

Implementation Aspects of OO-Languages

Umbrello UML Modeller Handbook

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Computing Concepts with Java Essentials

Introduction to Java

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Specialized Programme on Web Application Development using Open Source Tools

Object Oriented Software Design

Structural Design Patterns Used in Data Structures Implementation

Crash Course in Java

Programming Languages Featherweight Java David Walker

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

Transcription:

Reusing Classes in Java Composition A new class is composed of object instances of existing classes Java - Inheritance/Polymorphism/Interface CS 4354 Fall 2012 Jill Seaman Fields/members of one class contain objects from another. Name class can be made up of three Strings (first, middle, last), Student class can contain a Name object and other Strings. Inheritance Creates a new class as a type of an existing class Adds code to it without modifying the existing class All classes inherit from java standard class java.lang.object 1 2 Simple Example of Composition Inheritance class WaterSource { private String s; WaterSource() { System.out.println("WaterSource()"); s = new String("Constructed"); public class SprinklerSystem { private String valve1, valve2, valve3, valve4; private WaterSource source; SprinklerSystem() { System.out.println("SprinklerSystem"); valve1 = "v1"; source = new WaterSource(); A way to reuse code from existing objects by extending an existing class with new attributes and methods Classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, parent classes or ancestor classes. The resulting classes are known as derived classes, subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy. In Java, each class has exactly one superclass. If none are specified, then java.lang.object is the superclass. 3 4

Simple Example of Inheritance Simple Example of Inheritance class Cleanser { private String s = new String("Cleanser"); public void append(string a) { s += a; public void dilute() { append(" dilute()"); public void apply() { append(" apply()"); public void scrub() { append(" scrub()"); public String tostring() { return s; Cleanser x = new Cleanser(); x.dilute(); x.apply(); x.scrub(); System.out.println(x); Cleanser dilute() apply() scrub() tostring is a method of java.lang.object Implicit conversion of x to string This calls x.tostring() 5 public class Detergent extends Cleanser { extends is used to // Change (override) a method: specify the base-class public void scrub() { append(" Detergent.scrub()"); super.scrub(); // Call base-class version // Add methods to the interface: public void foam() { append(" foam()"); // Test the new class: Detergent x = new Detergent(); x.dilute(); x.apply(); x.scrub(); x.foam(); System.out.println(x); Cleanser.main(args); Cleanser dilute() apply() Detergent.scrub() scrub() foam() Cleanser dilute() apply() scrub() 6 General convention Some things you can do in a subclass Fields/attributes are private Not even subclasses should access these directly Methods are public This is so other classes, including subclasses can access them. Overriding a method: Writing a new instance method in the subclass that has the same signature as the one in the superclass. Any instance of the subclass will use the method from the subclass Any instance of the superclass will use the method from the superclass 7 The inherited fields can be used directly, just like any other fields (unless they are private). You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. 8

Initialization Java automatically inserts calls to the base-class constructor in the derived-class (subclass) constructor class Art { Art() { System.out.println("Art constructor"); class Drawing extends Art { Drawing() { System.out.println("Drawing constructor"); public class Cartoon extends Drawing { public Cartoon() { System.out.println("Cartoon constructor"); Cartoon x = new Cartoon(); Art constructor Drawing constructor Cartoon constructor 9 Initialization If your class doesn t have default constructors, or if you want to call a base-class constructor that has an argument, you must explicitly write the calls to the base-class constructor using the super keyword and the appropriate argument list class Game { Game(int i) { System.out.println("Game constructor"); class BoardGame extends Game { BoardGame(int i) { super(i); System.out.println("BoardGame constructor"); public class Chess extends BoardGame { Chess() { super(11); System.out.println("Chess constructor"); 10 More about inheritance Packages Upcasting The type of an object is the class that the object is an instance of. Java permits an object of a subclass type to be treated as an object of any superclass type. This is an implicit type conversion called upcasting Any method taking a Game as an argument can also take a BoardGame When to use composition, when to use inheritance Usually, composition is what you want Use inheritance if you want the interface (public members) of the re-used object to be exposed Use inheritance if you want your new class to be able to be used in methods expecting the re-used class (if you need upcasting). 11 Classes can be grouped into packages package mypackage; import... public class SmallBrain {... Declares these classes to belong to a package called mypackage package statement must come first in the file. Other classes (outside of mypackage) wanting access to SmallBrain must import mypackage, or fully specify it: mypackage.smallbrain. Anytime you create a package, you implicitly specify a directory structure when you give the package a name: this file should be in a directory named mypackage 12

Packages: example To put your classes in a package called xx.mypackage: Declare the package on the first line of each java file package xx.mypackage; import... public class SmallBrain {... Put all the files in package xx.mypackage in the following directory:...src/xx/mypackage Make src the current directory: cd...src Access specifiers keywords that control access to the definitions they modify public: accessible to all other classes protected: accessible to classes derived from (subclasses of) the class containing this definition. Note: protected also provides package access. package (unspecified, default): accessible only to other classes in the same package private: accessible only from within the class in which it is defined To compile: To run: javac xx/mypackage/*.java java xx.mypackage.classa Assuming ClassA contains a main method 13 14 The final keyword Java s final keyword has slightly different meanings depending on the context, but in general it says This cannot be changed. Data To create named constants (primitive type): public static final int VAL_THREE = 39; Use static so the class does not recreate it for each instance If you create an object that is final, it only means the reference cannot change, but the contents of the object itself could private final Value v2 = new Value(22); Cannot assign v2 to something else, but you could change its fields The final keyword Methods The final keyword in a method declaration indicates that the method cannot be overridden by subclasses. You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. final ChessPlayer getfirstplayer() { return ChessPlayer.WHITE; v2.setvalue(25); 15 16

The final keyword Classes When you say that an entire class is final (by preceding its definition with the final keyword), you state that you don t want to inherit from this class or allow anyone else to do so. class SmallBrain { final class Dinosaur { int i = 7; int j = 1; SmallBrain x = new SmallBrain(); void f() { Polymorphism Upcasting: Permitting an object of a subclass type to be treated as an object of any superclass type. The ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior. Each subclass of Game defines bool gameover(); in its own way. It allows many types (derived from the same base type) to be treated as if they were one type, and a single piece of code to work on all those different types equally. One piece of code can display a certain dialog when the game is over, regardless of which type of game it is. 17 18 Upcasting example Wind is an Instrument class Instrument { System.out.println("Instrument.play() " + n); class Wind extends Instrument { System.out.println("Wind.play() " + n); public class Music { public static void tune(instrument i) { Wind flute = new Wind(); tune(flute); //upcasting flute:wind is upcast to Instrument for tune Wind.play() Middle C 19 What if we didn t have upcasting? Wind, Stringed and Percussion are Instruments class Instrument { System.out.println("Instrument.play() " + n); class Wind extends Instrument { System.out.println("Wind.play() " + n); class Stringed extends Instrument { System.out.println("Stringed.play() " + n); class Percussion extends Instrument { System.out.println("Percussion.play() " + n); 20

What if we didn t have upcasting? cont. We have to overload tune to work for each subclass of Instrument If we add a new instrument, we have to add a new tune function public class Music { public static void tune(wind i) { public static void tune(stringed i) { public static void tune(percussion i) { Wind flute = new Wind(); Stringed violin = new Stringed(); Percussion snaredrum = new Percussion(); tune(flute); // No upcasting tune(violin); tune(snaredrum); Wind.play() Middle C Stringed.play() Middle C Percussion.play() Middle C 21 But we do have upcasting: We can get the same effect with just one tune method public class Music { public static void tune(instrument i) { Wind flute = new Wind(); Stringed violin = new Stringed(); Percussion snaredrum = new Percussion(); tune(flute); // upcasting tune(violin); tune(snaredrum); Wind.play() Middle C Stringed.play() Middle C Percussion.play() Middle C 22 Extensibility Extensibility part 1 Lets go back to the polymorphic tune method, AND add some more methods and instruments 23 class Instrument { System.out.println("Instrument.play() " + n); String what() { return "Instrument"; void adjust() { class Wind extends Instrument { System.out.println("Wind.play() " + n); String what() { return "Wind"; void adjust() { class Percussion extends Instrument { System.out.println("Percussion.play() " + n); String what() { return "Percussion"; void adjust() { 24

Extensibility part 2 Extensibility part 3 class Stringed extends Instrument { System.out.println("Stringed.play() " + n); String what() { return "Stringed"; void adjust() { class Brass extends Wind { System.out.println("Brass.play() " + n); String what() { return Brass ; class Woodwind extends Wind { System.out.println("Woodwind.play() " + n); String what() { return "Woodwind"; 25 public class Music3 { // Doesn't care about type, so new types // added to the system still work right: public static void tune(instrument i) { public static void tuneall(instrument[] e) { for(int i = 0; i < e.length; i++) tune(e[i]); // Upcasting during addition to the array: Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() ; tuneall(orchestra); Wind.play() Middle C Percussion.play() Middle C Stringed.play() Middle C Brass.play() Middle C Woodwind.play() Middle C 26 Abstract methods and classes Purpose of the Instrument class is to create a common interface (public members) for its subclasses No intention of making direct instances of Instrument An abstract class is a class that cannot be instantiated, but it can be subclassed It may or may not include abstract methods. An abstract method is a method that is declared without a method body (without braces, and followed by a semicolon), like this: abstract void f(int x); If a class contains an abstract method, it must be declared to be an abstract class. Abstract methods and classes, example Any class that inherits from an abstract class must provide method definitions for all the abstract methods in the base class. Unless the derived class is also declared to be abstract The Instrument class can be made abstract: No longer need dummy definitions for abstract methods Programmer and compiler understand how the class is to be used. abstract class Instrument { private int i; // Storage allocated in each subclass abstract void play(string n); //subclass must define String what() { return "Instrument"; abstract void adjust(); //subclass must define 27 28

Interfaces Interfaces In the Java programming language, an interface is a form or template for a class: it can contain only abstract methods (no method bodies). Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces. An interface is a pure abstract class: no instance-specific items. An interface can also contain fields, but these are implicitly static and final To create an interface, use the interface keyword instead of the class keyword. The methods (and fields) are automatically public To use an interface, you write a class that implements the interface. A (concrete) class implements the interface by providing a method body for each of the methods declared in the interface. An interface can be used as a type (for variables, parameters, etc) Java permits an object instance of a class that implements an interface to be upcast to the interface type 29 30 Interfaces, example interface Instrument { void play(string n); // Automatically public String what(); void adjust(); class Wind implements Instrument { public System.out.println("Wind.play() " + n); public String what() { return "Wind"; public void adjust() { class Percussion implements Instrument { public System.out.println("Percussion.play() " + n); public String what() { return "Percussion"; public void adjust() { class Stringed implements Instrument { public System.out.println("Stringed.play() " + n); public String what() { return "Stringed"; public void adjust() { Had to change access of methods to public Classes MUST define ALL the methods 31 class Brass extends Wind { public System.out.println("Brass.play() " + n); public String what() { return "Brass"; class Woodwind extends Wind { public System.out.println("Woodwind.play() " + n); public String what() { return "Woodwind"; public class Music5 { public static void tune(instrument i) { //unchanged public static void tuneall(instrument[] e) { for(int i = 0; i < e.length; i++) tune(e[i]); Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() ; tuneall(orchestra); The rest of the code is the same as before Wind.play() Middle C Percussion.play() Middle C Stringed.play() Middle C Brass.play() Middle C Woodwind.play() Middle C 32

Multiple Inheritance A Class may have only one immediate superclass But it may have many ancestors A Class my implement any number of interfaces. This allows you to say an x is an A and a B and a C 33 interface CanFight { void fight(); Multiple Inheritance, example interface CanSwim { void swim(); interface CanFly { void fly(); class ActionCharacter { public void fight( ) {System.out.println("fight"); class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly { public void swim() {System.out.println("swim"); public void fly() {System.out.println("fly"); public class Adventure { public static void t(canfight x) { x.fight(); public static void u(canswim x) { x.swim(); public static void v(canfly x) { x.fly(); public static void w(actioncharacter x) { x.fight(); Hero h = new Hero(); t(h); // Treat it as a CanFight u(h); // Treat it as a CanSwim v(h); // Treat it as a CanFly w(h); // Treat it as an ActionCharacter 34 Interface or Abstract class? Implementing the Comparable Interface Interface Pro: can be implemented by any number of classes Con: each class must have its own code for the methods, common method implementations must be duplicated in each class Abstract Class Assume you want to sort an array (or ArrayList) of custom objects (instances of some class you created). The following static methods are available in the Java API: void Collections.sort(List<T> list) void Arrays.sort(Object [] a) // for ArrayLists // for static arrays Pro: subclasses do not have to repeat common method implementations, common code is in the abstract superclass Con: Cannot be multiply inherited. All elements in the list/array must implement the java.lang.comparable interface: int compareto(t o); //T is your custom class Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 35 36

Sorting with Comparable, example Sorting with Comparable, example (p2) import java.util.*; public class Student implements Comparable { String name; String major; int idnumber; float gpa; public Student(String name, String major, int idnumber, float gpa) { this.name = name; this.major = major; this.idnumber = idnumber; this.gpa = gpa; public String tostring() { return "Student: " + name + " " +major + " " + idnumber + " " + gpa; public int compareto(object rhs) { String rhsname = ((Student)rhs).name; return name.compareto(rhsname); 37 Student[] a = new Student[3]; a[0] = new Student("Doe, J","Math",1234,3.6F); a[1] = new Student("Carr, M","CS",1000,2.7F); a[2] = new Student("Ames, D","Business",2233,3.7F); System.out.println("Before: "); for (int i=0; i<a.length; i++) System.out.println(a[i]); Arrays.sort(a); System.out.println("After: "); for (int i=0; i<a.length; i++) System.out.println(a[i]); Before: Student: Doe, J Math 1234 3.6 Student: Carr, M CS 1000 2.7 Student: Ames, D Business 2233 3.7 After: Student: Ames, D Business 2233 3.7 Student: Carr, M CS 1000 2.7 Student: Doe, J Math 1234 3.6 38 Sorting with Comparable, sort by gpa Extending an Interface To sort by gpa, redefine compareto as follows: public int compareto(object rhs) { float rhsgpa = ((Student)rhs).gpa; return (gpa < rhsgpa? -1 : (gpa == rhsgpa? 0 : 1)); public interface DoIt { void dosomething(int i, double x); int dosomethingelse(string s); Suppose that later you want to add a third method to DoIt: Before: Student: Doe, J Math 1234 3.6 Student: Carr, M CS 1000 2.7 Student: Ames, D Business 2233 3.7 After: Student: Carr, M CS 1000 2.7 Student: Doe, J Math 1234 3.6 Student: Ames, D Business 2233 3.7 public interface DoIt { void dosomething(int i, double x); int dosomethingelse(string s); boolean diditwork(int i, double x, String s); If you make this change, all classes that implement the old DoIt interface will break because they don't implement the interface 39 40

Extending an Interface Solution: you could create a DoItPlus interface that extends DoIt. public interface DoItPlus extends DoIt { boolean diditwork(int i, double x, String s); Now users of your code can choose to continue to use the old interface or to upgrade to the new interface. 41