OOP: Key Concepts 09/28/2001 1

Similar documents
8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Java Object Oriented Design. CSC207 Fall 2014

2. The object-oriented paradigm!

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

ITI Introduction to Computing II

ITI Introduction to Computing II

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Programming Languages and Techniques (CIS120)

Inheritance. Transitivity

Object Oriented Programming. Java-Lecture 11 Polymorphism

Extending Classes (contd.) (Chapter 15) Questions:

C++ Important Questions with Answers

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Inheritance and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Programming Languages and Techniques (CIS120)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

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

Comments are almost like C++

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Computer Science II (20073) Week 1: Review and Inheritance

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Interfaces

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

CMSC 132: Object-Oriented Programming II

Programming Languages and Techniques (CIS120)

Inheritance and Polymorphism

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Inheritance and Polymorphism

Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Lecture Notes Chapter #9_b Inheritance & Polymorphism

CMSC 132: Object-Oriented Programming II

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 14 Abstract Classes and Interfaces

Example: Count of Points

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance (continued) Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Programming Languages and Techniques (CIS120)

Object typing and subtypes

Java Fundamentals (II)

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

1 Shyam sir JAVA Notes

Instance Members and Static Members

The Java Programming Language

Chapter 6 Introduction to Defining Classes

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

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

What is Inheritance?

core Advanced Object-Oriented Programming in Java

Exercise: Singleton 1

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

Pascal: operations on basic data structures (selection) (I) 2.3 Accessing and manipulating data

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Computer Science 210: Data Structures

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Introduction to Programming Using Java (98-388)

G Programming Languages - Fall 2012

CPS 506 Comparative Programming Languages. Programming Language

Binghamton University. CS-140 Fall Dynamic Types

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Lecture Notes on Programming Languages

VIRTUAL FUNCTIONS Chapter 10

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Classes Continued. Fundamentals of Java

Practice for Chapter 11

OBJECT ORİENTATİON ENCAPSULATİON

Java: introduction to object-oriented features

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Chapter 5 Object-Oriented Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

2. The object-oriented paradigm

Programming in C# Inheritance and Polymorphism

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

Instantiation of Template class

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Object-Oriented Programming Concepts

Inheritance and Subclasses

CS/ENGRD 2110 SPRING 2018

CA Compiler Construction

int[] a; a = new int[8]; a[0] = 1; a[1] = 1; for (int i = 2; i < 8; i++) a[i] = a[i-1] - a[i-2];

Object-oriented Programming. Object-oriented Programming

Class, Variable, Constructor, Object, Method Questions

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Inheritance Motivation

Transcription:

OOP: Key Concepts Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring 09/28/2001 1

Overview of Part 1 of the Course Demystifying Java: Simple Code Introduction to Java An Example of OOP in practice Object Oriented Programming Concepts OOP Concepts -- Advanced Hints and for Java I/O (Streams) in Java Graphical User Interface Coding in Java Exceptions and Exception handling This slide set 08/23/2000 Introduction to Java 2

Overview Objects and Classes Fields, Methods, and Constructors Method overloading and resolution Instance and class variables Object and class methods Visibility / Access Inheritance and Polymorphism Subclass and super class Constructor chaining Object reference casting Shadowed variable and overriding method Dynamic method binding Abstract method and abstract class Inheritance hierarchy, interfaces, and multiple inheritance Visibility / Access 08/23/2000 Introduction to Java 3

Classes and Objects 08/23/2000 Introduction to Java 4

Objects and Classes What is an object? An object is a model of some specific thing - physical or conceptual. Man is not an object; Adam is an object. (Unless we are actually talking about e.g. genders of the human kind.) There are different kinds of objects. What then is a class? A class is the construct to define a collection of objects - objects of the same kind. Man is a class; Adam is an object of the class. An object is a specific instance of a class. 08/23/2000 Introduction to Java 5

Java classes and objects In Java, the class construct conceptually gathers together the data fields and methods to define the state and behavior of each object (instance) of the class. The fields and the methods are collectively referred to as the members of the class. class Stack { // fields int [] root = new int[100]; int top = 0; // methods void push(final int x) { root[top++] = x; int pop() { return root[--top]; boolean isempty() { return top==0; 08/23/2000 Introduction to Java 6

Fields and Methods class Stack { // fields int [] root = new int[100]; int top = 0; // methods void push(final int x) { root[top++] = x; int pop() { return root[--top]; boolean isempty() { return top==0; Given a Stack object, we can push an integer onto it; we can pop the integer off the top of stack; and we can verify that the stack is empty. (using the methods) To create a Stack object (to make an instance of the class), we use the new operator on the class name Stack s = new Stack(); 08/23/2000 Introduction to Java 7

The new operator call Stack s = new Stack(); The new operator results in a call to the constructor method; a Stack constructor is a method to initialize a Stack object - newly created by the new operator call. A constructor is a method using the class name as the method name, and it has no return type. But if we do not provide a constructor, Java supplies a default constructor. The default constructor has no parameters and does not do anything. We may want to provide our own Stack constructor, to specify the stack size when we create the stack class Stack { int [] root;... // constructor... Stack(int size) { root = new int[size];... 08/23/2000 Introduction to Java 8

Constructors However when we provide a constructor, there will be no default constructor. We will not be able to say: Stack s = new Stack(); But have to say: Stack s = new Stack(100); // stack of size 100 Stack s2 = new Stack(1000); // bigger stack to conform to the specified constructor - the sequence of the parameter types must match. Unless we also provide another constructor to match the parameter types in the calling sequence... 08/23/2000 Introduction to Java 9

Constructors Stack class with more than one constructor... class Stack { int [] root; int top = 0; // constructors... Stack(final int size) { root = new int[size]; Stack() { root = new int[100]; // methods void push(final int x) { root[top++] = x; int pop() { return root[--top]; boolean isempty() { return top==0; Different constructors must have different sequence of parameter types. (That is how the compiler knows which one to call, when we have more than one constructor.) 08/23/2000 Introduction to Java 10

One constructor calling another... One constructor can also call another constructor. In our Stack example, we may want to have: class Stack { int [] root; int top = 0; // constructors... Stack(final int size) { root = new int[size]; Stack() { this(100); // Set default size to 100 // methods... void push(final int x) { root[top++] = x; int pop() { return root[--top]; boolean isempty() { return top==0; Note the syntax of how one constructor calls another. 08/23/2000 Introduction to Java 11

Method overloading Method overloading When the same method name is used to refer to more than one method, the name is overloaded. Java supports method overloading: the same method name, or constructor name may refer to more than one method (or constructor). Java resolves the method call by matching the method s signature. The signature of a method is the combination of the method name and the sequence of parameter types. class Stack { int sum(final int from, final int to) { int sum(final int tolevel) { int sum()_{ // sum up the whole stack.... Note that the signature of a method does not include the method s return type. 08/23/2000 Introduction to Java 12

Garbage collection and finalize Java applies a technique called garbage collection - to relieve the programmer from the task of managing memory space no longer in use. While Java acquires memory space to construct a new object upon each new operator call. With garbage collection, Java does NOT reclaim the memory space not in use, until it is deemed necessary. A class may optionally define a finalize method, which is called when the system reclaims the space in garbage collection. The method must be named finalize, and has no parameters and no return type. There is no guarantee whether or not the finalize method will be called, nor when it will be called. 08/23/2000 Introduction to Java 13

Instance variable & class variable The members of a class consist of fields and methods. Each field defines an instance variable of each object of the class, except when the field is declared to be static. class Circle { double x,y,r; // (x,y) coordinates and radius static int circle_count; // number of circles created Every object of the class has its own x,y coordinates and its own radius. Each field -x, y, r - identifies an instance variable of a Circle object. But if we want to keep a count of how many objects have been created in the class Circle... 08/23/2000 Introduction to Java 14

Instance variable & class variable We need to declare a static field. The static field circle_count is a class variable because there is only one copy of it for the whole class. It models an attribute of the class, not that of each object of the class. class Circle { double x,y,r; // (x,y) coordinates and radius static int circle_count; // number of circles created If each of the Circle constructors would increment circle_count, it would keep a count of how many Circle objects have been created - provided that we properly initialize it to zero. 08/23/2000 Introduction to Java 15

Class variable class Circle { double x,y,r; // (x,y) coordinates and radius static int circle_count = 0; // number of circles created Circle(double x, double y, double r) { this.x = x; this.y = y; this.z = z; circle_count = circle_count + 1; To get the number of Circle object created... System.out.print( Circles created: ); System.out.println(Circle.circle_count); Note that we refer to a static member field with just the class name (without a reference an object). Java also provides a special method for the initialization of static member fields... 08/23/2000 Introduction to Java 16

Static initializer The static initializer is a special method. The method must be declared static, and it has no name, no parameters, and no return type. class Circle { double x,y,r; // (x,y) coordinates and radius static int circle_count; // number of circles created Circle(double x, double y, double r) { static { circle_count = 0; // static initializer The static initializer method is invoked automatically, invoked once and once only when the class is loaded for the first time. 08/23/2000 Introduction to Java 17

Object method & class method The members of a class consist of methods. Each member method defines an object method, unless it is declared static. In that case, the method is called a class method. class Circle { double x,y,r; // (x,y) coordinates and radius static final double PI = 3.1416; // Object method to compute area of a circle. double area() { return PI*r*r; // Class method to compare which of two circles is bigger. static Circle bigger(circle c1, Circle c2) { if (c1.r > c2.r) return c1; else return c2; area() is an object method; while bigger(c1,c2) is declared static, and is therefore a class method. 08/23/2000 Introduction to Java 18

Object method & class method Note the method calls Circle c1 = new Circle(0.0,0.0,3.0); double a1 = c1.area(); // area of circle c1. Circle c2 = new Circle(1.0,1.0,2.5); double a2 = c2.area(); // area of circle c2. Circle c3 = Circle.bigger(c1,c2); double a3 = c3.area(); // area of c3, the bigger circle. To invoke an object method, we must do so with a reference to an object of the class. But we can invoke a class method simply using the class name. In the method body which defines a class method, there is no this reference to refer to an object of the class. 08/23/2000 Introduction to Java 19

Access / Visibility In Java, we can apply access/visibility modifiers to specify the visibility of (or access to) the names declared for use. Other parts of the program can use a name only when the name is visible there (i.e., access to the name is permitted.) There are these modifiers: (default no modifier) public private protected These modifiers apply to class members, as well as classes. 08/23/2000 Introduction to Java 20

Visibility / Access Classes are partitioned into packages, and packages are organized hierarchically into a directory tree. Two visibility settings apply to classes: (default no modifier) visible to the package only public visible everywhere Four visibility settings apply to class members: (default no modifier) visible to the package only public visible everywhere private visible only within class scope* protected visible to the package and subclasses* Java source file must have file name extension ~.java. Each file allows only one public class, named by the same name as the file. 08/23/2000 Introduction to Java 21

Visibility / Access package X; public class Circle { private double x,y,r; // (x,y) coordinates and radius protected static int circle_count = 0; public static final double PI = 3.1416; public Circle(int x, int y, int r) { public double area() { return PI*r*r; public static Circle bigger(circle c1, Circle c2) { if (c1.r > c2.r) return c1; else return c2; // End of class scope ------- class T { static void main(string[] as) { Circle c1; // OK class Circle is public c1 = new Circle(0.0,0.0,2.5); // OK public ctor. double radius = c1.r; // ERROR: private, no access. double r_square = c1.area()/circle.pi; // OK. int num_circles = Circle.circle_count; // ERROR. 08/23/2000 Introduction to Java 22

Visibility / Access Modifier (no modifier) public private protected Class Visible in package. Visible everywhere. Not applicable. Not applicable. Class Member Visible in package. Visible everywhere. Visible only within class scope. Visible in package and subclasses.* 08/23/2000 Introduction to Java 23

Inheritance and Polymorphism 08/23/2000 Introduction to Java 24

Inheritance and Polymorphism Subclass and super class Constructor chaining Object reference casting Shadowed variable and overriding method Dynamic method binding Abstract method and abstract class Inheritance hierarchy Interfaces Multiple inheritance Visibility / Access 08/23/2000 Introduction to Java 25

Subclass and Super class A class is a construct to define a collection of objects objects of the same kind. A class construct may be derived to define a subset of that collection. We call it a subclass of the super class. In Java, a subclass extends the super class. class Circle { double x,y,r; double area() { class GraphicCircle extends Circle { Color outline, inside; Every object of the subclass GraphicCircle can also be treated as an object of the super class Circle 08/23/2000 Introduction to Java 26

Subclass and Super class There may be differences in response and behavior; every object of the subclass GraphicCircle can also be treated as an object of the super class Circle, but NOT the other way round. An object of GraphicCircle specifies the Color of its outline and inside, the object also has its center and radius specified by x,y and r, as if an object of Circle. GraphicCircle gc = new GraphicCircle( ); Color c_outside = gc.outline; Color c_inside = gc.inside; double center_x = gc.x; double center_y = gc.y; double radius = gc.r; double area = gc.area(); // OK also for method. 08/23/2000 Introduction to Java 27

Subclass and Super class Since we can refer the members of the super class as if they are members of the subclass, we say that the subclass inherits from the super class. Therefore, the construction of a GraphicCircle object (of the subclass) must also imply the construction of a Circle object (of the super class). In Java, the constructor of a subclass implicitly calls the constructor of the super class, if not done explicitly. NOTE in the following example, the places where an implicit call to a super class constructor may be inserted. This is known as Constructor chaining. 08/23/2000 Introduction to Java 28

Constructor chaining Constructor chaining class Circle { Circle(double x, double y, double r) { this.x = x; this.y = y; this.r = r; Circle() { this(0.0,0.0,1.0); class GraphicCircle extends Circle { GraphicCircle(double x, double y, double r, Color outside, Color inside) { super(x,y,r); // Explicit ctor call to super class. this.outline = outside; this.inside = inside; GraphicCircle(Color out, Color in) { this.outline = out; // Note - inserted: super(); this.inside = in; GraphicCircle() { this(black,white); // Nothing inserted. 08/23/2000 Introduction to Java 29

Constructor chaining finalizer? The construction of a subclass object must imply the construction of a super class object therefore the constructor calls are chained from subclass to super class. The same style of chaining does NOT apply to the finalizer calls (to finalize method). Java does not insert any implicit call to the finalize method of the super class, but it can be done explicitly with super.finalize(); Java does not guarantee the calling of the finalize methods, nor the order in which they may follow. 08/23/2000 Introduction to Java 30

object casting When class B extends class A, we can refer to an object of class B in terms of the class A object. This can happen by explicit casting, or implicit conversion in assignment, argument passing, or method return. B extends A B b; A a; ((A)b).i A a = b; // Explicit casting // Implicit conversion in assignment It is OK because we can treat as object of a subclass as if it is an object of the super class. We can treat an object of a subclass as if an object of the super class; but NOT the other way round. 08/23/2000 Introduction to Java 31

Shadowed variable The subclass inherits the fields from the super class, but can also define its own fields. If the subclass defines a field using the same name as an inherited field, the field from the subclass becomes a shadowed variable in the subclass it becomes inaccessible unless requested explicitly. class A { int i = 1; class B extends A { int i = 2; int f() { return i; // my own i in B: 2. int g() { return super.i; // i from A: 1. B b = new B(); System.out.println(b.f()); // prints out 2. System.out.println(b.g()); // prints out 1. 08/23/2000 Introduction to Java 32

Overriding Method The subclass inherits the methods from the super class, but can also define its own methods. If the subclass defines a method with the same signature as an inherited method, the method is called an overriding method, because the inherited method becomes inaccessible unless explicitly requested. class A { int f() { return 1; class B extends A { int f() { return 2; int g() { return super.f(); B b = new B(); System.out.println(b.f()); // prints out 2. System.out.println(b.g()); // prints out 1. 08/23/2000 Introduction to Java 33

Shadowed Variable & Overriding Method Class A { int i = 1; int f() { return i; Class B extends A { int i = 2; int f() { return i; B b = new B(); System.out.println(b.i); // 2 System.out.println(b.f()); // -2 A a = b; // treat it an object of class A. System.out.println(a.i); // 1 System.out.println(a.f()); // -2 08/23/2000 Introduction to Java 34

Dynamic method binding Since class B extends class A, we can treat an object of class B as if it is an object of class A. A a = b; // Treat object b as if it is of class A. When treated as an object of class A, the name of a shadowed variable will refer to that of class A. Hence, a.i gives 1. But the name of an overriding method now refers to the method defined for the class the object actually is, i.e., class B. Thus, a.f() gives 2. Polymorphism is the phenomenon in which treating similar objects in the same way leads to different responding behavior according to the difference in objects. Java supports polymorphism by dynamic method binding. 08/23/2000 Introduction to Java 35

Dynamic method binding Dynamic method binding class Shape { double s; double area() { return 1.0; Shape(final double s) { this.s = s; class Circle extends Shape { final double PI = 3.1416; double area() { return PI*s*s; Circle(final double r) { super(r); class Square extends Shape { double area() { return s*s; Square(final double s) { super(s); class Rectangle extends Square { double oside; double area() { return s*oside; Rectangle(final double s, final double oside) { super(s); this.oside = oside; 08/23/2000 Introduction to Java 36

Dynamic method binding class Shape { class Circle extends Shape { class Square extends Shape { class Rectangle extends Square { Shape c = new Circle(3.0); Shape s = new Square(5.0); Shape r = new Rectangle(4.0,6.0); double ca = c.area(); // 28.3 of Circle double sa = s.area(); // 25.0 of Square double ra = r.area(); // 34.0 of Rectangle Observe that the correct area() method was called. 08/23/2000 Introduction to Java 37

Abstract method Note that the area() method of class Shape is not used at all. In fact, the method is meaningless for the class. We need the area() method only because we want to have a uniform way to treat its subclasses. In other words, area of a shape makes sense only when it is a real shape. Java allows us to declare a method, but supply no method body to define its behavior it is therefore called an abstract method. abstract class Shape { double s; abstract double area(); // No method body. Shape(final double s) { this.s = s; 08/23/2000 Introduction to Java 38

Abstract class A class with at least one abstract method is called an abstract class. (The modifier abstract is optional.) There can be NO instances of an abstract class. But a subclass can implement overriding methods. The subclass is not abstract (concrete) when all abstract methods have an overriding method implemented. We can then have objects (instances) of the subclass, and treat them as if they were objects of the abstract class. NOTE therefore in the inheritance hierarchy: the super class of an abstract class is always an abstract class; the subclass of a non-abstract (concrete) class is always a nonabstract (concrete) class. 08/23/2000 Introduction to Java 39

Inheritance hierarchy In Java, each class can have at most one and only one super class. That is, a class cannot inherit from more than one class. In Java, there is a class Object each class is automatically a descendant of the class Object, that is, each class is either a subclass of Object, or that it is a descendant of some subclass of Object. Hence, in Java, the inheritance hierarchy shows a tree rooted at the class Object. 08/23/2000 Introduction to Java 40

Interface An abstract class represents the way we can treat the objects of its subclasses. The form of treatment as represented in the collection of methods is partially implemented. An interface is similar to an abstract class interface Drawable { static final double PI=3.1416; void setcolor(color c); void setposition(double x, double y); void draw(window w); All the methods of the interface must be abstract; furthermore, all the fields must be static and final. 08/23/2000 Introduction to Java 41

Multiple Inheritance and Interfaces Many OOPL use multiple inheritance to give object characteristics of multiple parents. Useful, but potentially a landmine if both parents have a method of the same name. For these reasons Java does not allow multiple inheritances. Java uses interfaces (which are constrained in some special ways to avoid this problem) to allow for the equivalent of multiple inheritance. Interfaces are implemented frequently as a means for providing class hierarchies. 08/23/2000 Introduction to Java 42

Interface An interface represents a way to treat objects. When a class implements overriding methods for all the abstract methods in an interface, in Java, we say the class implements the interface. class Circle { double x,y,r; static final double PI=3.1416; double area() { return PI*r*r; class DrawableCircle extends Circle implements Drawable { Color c; void setcolor(color c) { this.c = c; void setposition(double x, double y) { this.x = x; this.y = y; void draw(window w) { w.draw( ); 08/23/2000 Introduction to Java 43

Interface When a class implements an interface, we can treat the objects of the class according to the treatment represented in the interface apply the methods of the interface on the object. Hence, we can have object reference for an interface referring to objects of the class implementing the interface. Class DrawableCircle implements interface Drawable Drawable dx = new DrawableCircle( ); dx.setcolor(yellow); dx.setposition(10.0,10.0); dx.draw(desktop_window); 08/23/2000 Introduction to Java 44

Interface Much like subclass extending an abstract class, we can extend an interface into another interface (sometimes called sub-interface) as long as we continue to observe the rules of abstract methods and constant fields. The extended interface is still an interface. interface Drawable { void setcolor(color c); void setposition(double x,double y); void draw(window w); interface Fillable extends Drawable { void setimage(image); void setregion(polygon); void fill(window); 08/23/2000 Introduction to Java 45

Multiple interfaces An important result of the restriction in interfaces (compared to abstract classes) is that an interface can unambiguously extend multiple interfaces. interface Draw_and_HighLight extends Drawable, HighLight { ; A class can also implement multiple interfaces. class LightedButton extends Shape implements Drawable, HighLight { // must implement all Drawable methods // must implement all HighLight methods 08/23/2000 Introduction to Java 46

Visibility / Access The same visibility / access modifiers apply Modifier (no modifier) public private protected Class / Interface Visible in package. Visible everywhere. Not applicable. Not applicable. Class Member ( including interface method ) Visible in package. Visible everywhere. Visible only within class scope. Visible in package and subclasses. 08/23/2000 Introduction to Java 47