JOI - Java Objects by Interface

Similar documents
Chapter 10 Classes Continued. Fundamentals of Java

Object-Oriented Programming

Object Orientation Fourth Story. Bok, Jong Soon

Java Primer 1: Types, Classes and Operators

Programming overview

Introduction to Programming Using Java (98-388)

Chapter 14 Abstract Classes and Interfaces

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

C++ Inheritance and Encapsulation

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

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Chapter 6 Introduction to Defining Classes

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

A Short Summary of Javali

CS 251 Intermediate Programming Inheritance

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Lecture Notes on Programming Languages

Singleton Pattern Creational

Decaf Language Reference Manual

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CLASSES AND OBJECTS IN JAVA

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Metamodeling with Metamodels. Using. UML/MOF including OCL

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Informatica 3 Syntax and Semantics

CPS 506 Comparative Programming Languages. Syntax Specification

Inheritance and Polymorphism

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

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

COP 3330 Final Exam Review

What are the characteristics of Object Oriented programming language?

Inheritance. Transitivity

Inheritance and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

C++ Inheritance and Encapsulation

And Even More and More C++ Fundamentals of Computer Science

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

5.6.1 The Special Variable this

Inheritance (Part 5) Odds and ends

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Lecture 02, Fall 2018 Friday September 7

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Chapter 7)

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

BBM 102 Introduction to Programming II Spring Inheritance

Java: introduction to object-oriented features

What is Inheritance?

Object Orientated Analysis and Design. Benjamin Kenwright

Practice for Chapter 11

Programming Language Concepts: Lecture 2

Chapter 7. Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance, and Polymorphism.

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

Self-review Questions

Intermediate Code Generation

Sub- PPL Unit-I Class-SE Comp

CSE 70 Final Exam Fall 2009

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Fundamental Concepts and Definitions

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Inheritance and Polymorphism

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

Java Programming Tutorial 1

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Lecture 18 Tao Wang 1

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

Cpt S 122 Data Structures. Introduction to C++ Part II

Logistics. Final Exam on Friday at 3pm in CHEM 102

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

Object-Oriented Design

Synchronization SPL/2010 SPL/20 1

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

Compaq Interview Questions And Answers

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

15CS45 : OBJECT ORIENTED CONCEPTS

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

Syntax. A. Bellaachia Page: 1

Experiences in Building a Compiler for an Object-Oriented Language

Object-Oriented Concepts and Design Principles

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 3. Describing Syntax and Semantics

Chapter 10 Introduction to Classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

C++ Important Questions with Answers

JAVA GUI PROGRAMMING REVISION TOUR III

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming II (CS300)

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

Java Threads and intrinsic locks

R/3 System Object-Oriented Concepts of ABAP

CS18000: Programming I

Exam Duration: 2hrs and 30min Software Design

Transcription:

JOI - Java Objects by Interface Heiko von Drachenfels, Oliver Haase, and Robert Walter HTWG Konstanz - University of Applied Sciences, Constance, Germany drachenfels haase rwalter@htwg-konstanz.de 1 Introduction Object-orientation has long been equated with inheritance. Nowadays, however, it is widely aknowledged that inheritance can lead to undesirable dependencies between super and subclasses. Due to these dependencies, seemingly correct changes to a base class can lead to malfunctioning subclasses. This effect is known as the fragile base class problem. As a consequence, programmers are advised to avoid inheritance and replace it with object composition and delegation. Another technique to reduce dependencies between classes is to consequently separate implementations (classes) from interfaces. Ideally, a class, c, that uses an object, o, should not know the actual implementation of o, but only the interface(s) that o provides. If c is consequently programmed against interfaces rather than implementations, replacing the actual implementations of the objects that c uses becomes trivial. Exchanging one class (implementation) without affecting other classes is a key requirement of truly modular software components, whose benefit becomes especially obvious during testing. Both techniques, the avoidance of inheritance and the strict separation of interface and implementation can readily be applied in object-oriented languages such as Java, C++, and C#. The problem, however, is that these techniques are (to some extent) supported, but they are not enforced. In general, a good programming language should not only allow for a good programming style (which is possible with most languages, given the proper amount of self discipline), but it should enforce it. In this paper, we present Joi (Java Objects by Interface), a Java extension that enforces the two programming techniques described above. Joi is a programming language that builds upon Java in that it uses Java syntax to a large extent and adds a few own keywords. A Joi program consists of (genuine Java) interfaces and so-called components which provide implementations for the interfaces. Components cannot be directly instantiated, but objects can only be gotten through factory methods and appear as instances of interface types. Making objects only available through (Java) interfaces has several implications that lead to a cleaner object-oriented approach. For one, because interfaces cannot contain member variables, a component s member variables cannot be accessed from the outside and thus are automatically private. This not only enforces a better programming style, but also makes the need for explicit access right modifiers obsolete. Secondly, because an interface cannot contain static methods, all functionality has to be modeled as instance methods. Since in a

purely object-oriented approach, static methods and static variables are modeled as members of singleton objects, Joi directly supports the singleton pattern, by taking care that the factory methods of a singleton component always return the only existing instance. A Joi component cannot only use other joi components, but it can also instantiate and use plain vanilla Java objects. The other way around, a Java object can use a Joi component s factory method to get access to a Joi object and subsequently use it. That is, Joi and Java can be mixed together and inter-work, allowing for an evolutionary migration from existing Java code to newly developed Joi code. 2 Language Definition On the level of method definitions, Joi uses Java syntax (and semantics). The differences between Joi and Java lie in the following aspects: A Joi component must provide (implement) at least one interface, because Joi objects are only seen through the interface types they provide. A Joi component not providing any interface would be unusable. There are no access right modifiers in Joi, because the access rights are defined through the interfaces a component provides: All instance variables and constructors are private because they cannot be included in an interface, all instance methods are public, and the special main method is implicitly made public. There are no static class members, they only exception being the main method. For each combination of provided interface and defined constructor, Joi generates a (static) factory method with the same parameters as the constructor that return an instance of the interface type. If, e.g., a component MyComponent provides the interface MyInterface and defines a constructor MyComponent(String name, int[] numbers), then Joi automatically generates a factory method MyInterface getmyinterface(string name, int[] numbers). These factory methods are the only means to get access to a Joi object. Joi has built-in support for the singleton pattern. If a component is defined as singleton, the factory methods always return the same singleton object. This support is particularly useful, because static variables and methods need to be modeled as singleton variables and methods. The formal syntax definition of Joi in EBNF (Extendend Backus-Naur Form) is presented and explained in the following section. 2.1 EBNF for Joi To keep the EBNF simple, we only show the parts that differ from Java and presume the Java elements to be well known. More specifically, this comprises the following non-terminals:

<PACKAGE_DIRECTIVE>: The package the component belongs to. <IMPORT>: A Java style import statement. <MAIN>: The main method in Java syntax, except that the signature comes without modifiers (public, static) and without return value (void). <INTERFACE_NAME>: A type name in Java syntax. <INSTANCE_METHOD>, <INSTANCE_VARIABLE>, <CONSTRUCTOR>. Same as in Java, except without access right modifiers. Based on the above presumptions, a simplified Joi-EBNF is shown in the following listing 1.1. As usual, the notation [ s ] stands for an optional occurrence of s, while { s denotes arbitrary many (including zero) repetitions of s. Listing 1.1. Joi - EBNF CompileUnit = [ < PACKAGE_ DIRECTIVE > ] { < IMPORT > Component ; Component = [ " singleton " ] " component " <ID > " provides " Interfaces " { " { Member [ < MAIN > ] " " ; Interfaces = < INTERFACE_ NAME > [ "," Interfaces ] ; Member = < INSTANCE_ METHOD > < INSTANCE_ VARIABLE > < CONSTRUCTOR > ; A compile unit can start with a package declaration, followed by either none or an arbitrary amount of package imports. A component optionally starts with the keyword singleton and has to provide at least one interface. The members of a component are instance methods, instance variables and constructors, in arbitrary order. If the class defines a main method, it has to go last in the component definition. 2.2 Simple Example In this section, we illustrate the use of Joi with a simple example. As each Joi component must provide at least one interface, we start with an interface definition as shown in listing 1.2. Please note that the interface is defined in plain vanilla Java. package a r t i c l e e x a m p l e s ; 2 public interface WelcomeIF { 4 void warmwelcome ( S t r i n g name ) ; Listing 1.2. Interface WelcomeIF As can be seen, the interface WelcomeIF contains only one method warmwelcome with a parameter name of type String and no return value. Listing 1.3 shows a simple Joi component JoiWelcome which provides the java interface WelcomeIF by implementing the method warmwelcome.

1 package a r t i c l e e x a m p l e s ; Listing 1.3. Joi component JoiWelcome 3 component JoiWelcome provides WelcomeIF { JoiWelcome ( ) { 5 void warmwelcome ( S t r i n g name ) { 7 System. out. p r i n t l n ( " P l e a s e d to meet you, " + name + ". I hope you will enjoi it here! " ) ; 9 11 main ( S t r i n g [ ] a r g s ) { i f ( a r g s. l e n g t h!= 1) { 13 System. out. p r i n t l n ( " U s a g e : java J o i W e l c o m e < YourName > " ) ; System. e x i t ( 1 ) ; 15 17 WelcomeIF w i f = JoiWelcome. getwelcomeif ( ) ; w i f. warmwelcome ( a r g s [ 0 ] ) ; 19 The component JoiWelcome contains a main method that shows the use of the generated factory method getwelcomeif to get an instance of type WelcomeIF. At this instance wif, only the methods defined in WelcomeIF can be called, in our case the method warmwelcome. 3 Implementation A Joi component MyComponent is defined in a file named MyComponent.joi. In our current implementation, the Joi compiler joic converts the Joi source file into a Java source file containing a Java class with the same name, MyComponent, which hides the implementation details in a nested class. After running joic, the java source file can be compiled into byte code and executed by a Java virtual machine, as usual. Listing 1.4 shows the result of applying joic to the Joi component JoiWelcome in listing 1.3. package a r t i c l e e x a m p l e s ; 2 public f i n a l class JoiWelcome { 4 private JoiWelcome ( ) { Listing 1.4. Generated Java class JoiWelcome 6 public s t a t i c WelcomeIF getwelcomeif ( ) { return ( WelcomeIF ) new Implementation ( ) ; 8 10 private s t a t i c f i n a l class Implementation implements WelcomeIF { private Implementation ( ) { 12 public void warmwelcome ( S t r i n g name ) { System. out. p r i n t l n ( " P l e a s e d to meet you " + name + 14 ". I hope you will enjoi it here! " ) ; 16 18 public s t a t i c void main ( S t r i n g [ ] a r g s ) { i f ( a r g s. l e n g t h!= 1) { 20 System. out. p r i n t l n ( " U s a g e : java J o i W e l c o m e < YourName > " ) ;

22 System. e x i t ( 1 ) ; 24 WelcomeIF w i f = JoiWelcome. getwelcomeif ( ) ; w i f. warmwelcome ( a r g s [ 0 ] ) ; 26 As can be seen, the interface WelcomeIF is implemented not by the outer class JoiWelcome, but by the nested class Implementation. The generated factory method getwelcomeif instantiates an object of the nested class Implementation and returns it as an instance of the interface type WelcomeIF. Please note that the only public method of the Java class JoiWelcome is the factory method. Thus, from a user perspective JoiWelcome is an utility class whose only use is to generate instances of type WelcomeIF. The use of the factory pattern minimizes the dependency between implementation provider and user. The user must code against an interface rather than against an implementation. The actual implementation can be exchanged by simply replacing one factory with another one that generates objects of the same interface type. Finally, it should be noted that Joi components could also directly be transformed into byte code, without the intermediate step of Java source code. We opted for transformation on the source code level, because it was easier to realize and because the generated Java source code nicely illustrates the ideas and mechanisms behind Joi.