Object-Oriented Programming, Part 2

Similar documents
Object-Oriented Programming, Part 2

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Packages and Information Hiding

Object Orientation Fourth Story. Bok, Jong Soon

Documentation of Sotfware

Code Listing H-1. (Car.java) H-2 Appendix H Packages

Packages Inner Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to Java

Programming overview

Basics of Java Programming. Hendrik Speleers

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java: introduction to object-oriented features

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

From C++ to Java. Duke CPS

Selected Java Topics

Introduction to Object-Oriented Programming

JAVA PROGRAMMERS GUIDE LESSON

Object-Oriented Programming Concepts

CS 351 Design of Large Programs Singleton Pattern

JAVA: A Primer. By: Amrita Rajagopal

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Defining Classes and Methods

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

Basics of Object Oriented Programming. Visit for more.

Java Professional Certificate Day 1- Bridge Session

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS260 Intro to Java & Android 03.Java Language Basics

Final Exam CS 251, Intermediate Programming December 10, 2014

ITI Introduction to Computing II

Class, Variable, Constructor, Object, Method Questions

Lecture 02, Fall 2018 Friday September 7

1 Shyam sir JAVA Notes

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Exploring the Java API, Packages & Collections

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

Class Libraries and Packages

Inheritance Motivation

Course information. Petr Hnětynka 2/2 Zk/Z

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

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

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

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Packages. Examples of package names: points java.lang com.sun.security drawing.figures

LECTURE 2 (Gaya College of Engineering)

Classes. Classes as Code Libraries. Classes as Data Structures

Java. Package, Interface & Excep2on

What is Inheritance?

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

core Advanced Object-Oriented Programming in Java

Lecture 10 Declarations and Scope

The Essence of OOP using Java, Nested Top-Level Classes. Preface

COMP6700/2140 Packages, Modules and Jigsaw

Building custom components IAT351

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

Object Orientated Analysis and Design. Benjamin Kenwright

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

The Singleton Pattern. Design Patterns In Java Bob Tarr

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

PIC 20A The Basics of Java

9 Working with the Java Class Library

Introduction to Programming Using Java (98-388)

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Inheritance and Interfaces

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

CHAPTER 1 Introduction to Computers and Java

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

School of Informatics, University of Edinburgh

Lecture 6 Introduction to Objects and Classes

An overview of Java, Data types and variables

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CSC207 Week 2. Larry Zhang

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

UFCE3T-15-M Object-oriented Design and Programming

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Lecture 7: Classes and Objects CS2301

Course information. Petr Hnětynka 2/2 Zk/Z

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

CSC116: Introduction to Computing - Java

Interfaces. An interface forms a contract between the object and the outside world.

Java Programming. Manuel Oriol, March 22nd, 2007

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Data Abstraction. Hwansoo Han

CMSC 341. Nilanjan Banerjee

Another IS-A Relationship

Lecture 13: Design Patterns

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

CS260 Intro to Java & Android 02.Java Technology

Transcription:

Object-Oriented Programming, Part 2 Packages Information hiding Access modifiers private protected public "friendly" OOP: Object-Oriented Programming, Part 2 1

Packages in Java A package is a collection of classes (a library). In a file the first line must specify the package, e.g., package mypackage; Characteristic of a package Organized in a hierarchy Uses the file system for implementing the hierarchy A package corresponds to a directory Every package is a name space By default, classes belong to the unnamed package. Packages introduce new scope rules. OOP: Object-Oriented Programming, Part 2 2

Packages in Java, Cont Typical problems with packages Tied to local directory structure Case sensitive Default package in current directory. Good design All classes should be in a explicit package, i.e., do not use the unnamed package (except for teaching :-)) OOP: Object-Oriented Programming, Part 2 3

Package Example package com.mycompany.misc; // file Car.java public class Car { public Car(){ System.out.println ("com.mycompany.misc.car"); package com.mycompany.misc; // file Truck.java public class Truck { public Truck(){ System.out.println ("com.mycompany.misc.truck"); OOP: Object-Oriented Programming, Part 2 4

Accessing Classes in Package A class MyClass in a package mypackage is accessed via mypackage.myclass This can be nested to any level mypackage1.mypackage2.mypackage3.myotherclass To avoid too much doting packages can be imported, e.g., In a file import mypackage1.mypackage2.mypackage3.*, then, MyOtherClass does not have to be qualified. If name clashes, i.e., same class name in two imported packages, then use fully qualified name. The package java.lang is always imported. OOP: Object-Oriented Programming, Part 2 5

Accessing Classes, Example One import java.lang.*; // not needed always done public class Garage1 { com.mycompany.misc.car car1; com.mycompany.misc.truck truck1; public Garage1(){ car1 = new com.mycompany.misc.car(); truck1 = new com.mycompany.misc.truck(); public void toprint(){ System.out.println ("A garage: " + Math.PI); from java.lang OOP: Object-Oriented Programming, Part 2 6

Accessing Classes, Example Two import com.mycompany.misc.*; import java.lang.*; // not needed always done public class Garage { Car car1; Truck truck1; public Garage(){ car1 = new Car(); truck1 = new Truck(); from com.mycompany.misc public void toprint(){ System.out.println ("A garage: " + Math.PI); from java.lang OOP: Object-Oriented Programming, Part 2 7

CLASSPATH Store misc package in /user/torp/java/com/mycompany/misc directory, i.e., the files Car.class and Truck.class. CLASSPATH = /user/torp/java;/user/torp/something.jar;etc Compiler starts search at CLASSPATH OOP: Object-Oriented Programming, Part 2 8

Information Hiding Separate interface from implementation How much should a user of a class see? Rules of thumb Make instance variables private Make part of the methods public OOP: Object-Oriented Programming, Part 2 9

Access Modifiers on Variables/Methods private Variable/method is private to the class. "Visible to my self". protected Variable/method can be seen by the class, all subclasses, and other classes in the same package. "Visible to the family" or "beware of dog". public Variable/method can be seen by all classes. "Visible to all". OOP: Object-Oriented Programming, Part 2 10

"Friendly" Default access, has no keyword. public to other members of the same package. private to anyone outside the package. "Visible in the neighborhood" Also called package access. OOP: Object-Oriented Programming, Part 2 11

Public/"Friendly" Example package com.mycompany.misc; public class Car { public Car(){ System.out.println ("com.mycompany.misc.car"); void foo () { System.out.println ("foo"); package mynewpackage; // in another package import com.mycompany.misc.*; public static void main (String args[]){ Car car1 = new Car(); car1.foo(); // compile error "private" in this package OOP: Object-Oriented Programming, Part 2 12

Private Example class Singleton { private int mydata = 42; private static Singleton s = new Singleton(); // make default constructor private private Singleton(){ public static Singleton getsingleton () { return s; public void singletonmethod() { /* do stuff */ ; public int getsingletondata { return mydata; public class UseSingleton { public static void main (String args[]){ Singleton s1 = new Singleton(); // compile error Singleton s2 = Singleton.getSingleton(); s2.singletonmethod(); OOP: Object-Oriented Programming, Part 2 13

Singleton Design Pattern Singleton static getinstance() singletonmethod() getsingletondata() return uniqueinstance; static uniqueinstance otherdata Controlled access to one instance Reduced name space (not a "global" variable) Permits refinement of operations and representation Permits a variable number of instances More flexible than static methods Very good object-oriented design OOP: Object-Oriented Programming, Part 2 14

private Access Modifiers on Classes Not supported in Java! Default see the slide on friendly protected Not supported in Java! public Can be seen by all other classes in other packages Only one public class per file "Friendly" A class without an access modifier can be accessed from the classes within the same package. There are no access modifiers on packages What would it mean? OOP: Object-Oriented Programming, Part 2 15

Tools The Java Development Kit (JDK) contains a set of tools useful for Java programmers. javac The Java compiler Translates Java source code to byte code javac [options] <filename.java> java The Java interpreter Interprets Java byte code java [options] <filename.class> javadoc The Java documentation generator javadoc [options] <filename.class> <package> OOP: Object-Oriented Programming, Part 2 16

Javadoc, Example import com.mycompany.misc.*; /** Implements a garage consisting of cars and trucks */ public class Garage { /** Comment on instance variable */ Car car1; Truck truck1; /** Default constructor, create a car and a truck */ public Garage(){ /** Comment on method internals */ car1 = new Car(); truck1 = new Truck(); /** Prints the vehicles made in the constructor * @see Garage#Garage() */ public void toprint(){ System.out.println ("A garage: " + car1 + truck1); OOP: Object-Oriented Programming, Part 2 17

Class Properties A class variable is a variable that is common to all instances of the same class. A class method is a method that operates on a class as it was an object. Classes are objects (metaobjects) Class variables are stored in metaobjects Java supports metaobject via the class Class. Further there is a set of classes in the package java.lang.reflect. See Chapter 12 "Run- Time Type Identification". OOP: Object-Oriented Programming, Part 2 18

Class Properties, cont. Variables marked with static are class variables. public static float tax = 22.75; Methods marked with static are class methods public static void main (String args[]){ The Math class consists entirely of static methods and variables. We never construct a Math object. In general that is not a good design. OOP: Object-Oriented Programming, Part 2 19

Summary Package, the library unit in Java. Access modifiers Tells clients what they can and cannot. Separation of interface from implementation. Very important in design (and implementation). Guideline: Make elements as hidden as possible. Object-oriented design hard parts Decomposing system into objects. Defining the public interface of the objects. Finding out what is likely to change. Finding out what is likely to stay the same. OOP: Object-Oriented Programming, Part 2 20