Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): A computational unit consisting of some data elements.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 884 (Prasad)Java Classes1 Classes Fields, methods, and constructors Inheritance.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Classes, Interfaces and Packages
CS 884 (Prasad)Java Names1 Names Scope and Visibility Access Control Packages.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
OOP: Encapsulation &Abstraction
JAVA MULTIPLE CHOICE QUESTION.
Inheritance-Basics.
Inheritance and Polymorphism
Java Programming Language
Packages and Interfaces
The Building Blocks Classes: Java class library, over 1,800 classes:
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Java Classes and Inheritance
Java Classes and Inheritance
Applying OO Concepts Using Java
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Lecture 10 Concepts of Programming Languages
Chapter 5 Classes.
Presentation transcript:

cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing encapsulation

cs3180 (Prasad)L8Packages2 Organization of a Java Program Java Program –Forest of packages E.g., $PWD, java, javax, etc. Package (Tree) –Directory of sub-packages and compilation units E.g., java.io, java.util, java.lang.Thread, java.applet.Applet; Compilation Unit (Leaf) – File containing class and interface definitions with at most one public class CLASSPATH environment variable –Set to full-path names of roots of trees in the forest

cs3180 (Prasad)L8Packages3 CLASSPATH = (Sub-)Package = Compilation Unit

cs3180 (Prasad)L8Packages4 package statement –Omitted : anonymous package For *.class byte code files to be accessible to the JVM, ensure “.” ($PWD) is contained in the environment variable CLASSPATH. –package Pk; If a class is declared inside package Pk (or subpackage Pk.Sk ), then the *.java source files must be stored in the directory Pk (or Pk/Sk), and the CLASSPATH must contain at least.../Pk.

cs3180 (Prasad)L8Packages5 import statement ( for programmer convenience ) –importing a type explicitly import java.util.StringTokenizer; –importing type-on-demand import java.io.*; (**New in Java 5**) –importing static members individually import static java.lang.Math.PI; –importing static members in group import static java.lang.Math.*;

cs3180 (Prasad)L8Packages6 –private : inaccessible from outside the class –default : accessible in the package –protected : accessible in the subclasses and the package –public : no restriction on accessibility Access Control : Informal Spec

cs3180 (Prasad)L8Packages7

cs3180 (Prasad)L8Packages8 class S extends C { public int i; protected int j; private int k; int p(C c) { return c.x ; // + c.y + c.z ; } int q(S s) { return s.x + s.y + // s.z + s.i + s.j + s.k ; }} class C { public int x; protected int y; private int z; int p(C c) { return c.x + c.y + c.z ; } int q(S s) { return s.x + s.y +((C) s).y +((C) s).z // + s.z + s.i ; // + s.j + s.k ; }} Access Control : C and S in different packages

Notes Public, protected and private fields of a direct instance (e.g., c.x, c.y, c.z ) of a class are accessible in the class (C’s text), while the private fields of a subclass instance (e.g., z field of s ) are accessible in the class (C’s text) only via a static cast (e.g., ( (C)s).z, and not s.z ). The inherited protected field due to parent class (C) of a direct instance of a subclass (e.g., s.y ) is available in subclass (S’s text). –Note that the protected field of a direct class instance (e.g., c.y ) is not available in the subclass (S’s text) and the protected field of a subclass instance is not available in the parent class (C’s text) (e.g., s.j ). cs3180 (Prasad)L8Packages9

cs3180 (Prasad)L8Packages10 Other Constraints Access modifier of a hiding/overriding method must provide at least as much access as the hidden/overridden method. Otherwise, access barrier beaten by casting. class C { public void p() {} } class S extends C { private void p() {} } C x = new S(); x.p(); Illegal Code

cs3180 (Prasad)L8Packages11 Hiding and Overriding : Static and Dynamic Binding S x = new S(); C y = x; (x.a == 77) (y.a == 84) (((C) x).a == 84) (x.p() == y.p()) (x.q() != y. q()) (((C) x).q() == y.q()) class C { int a = 84; static int q() {} int p() {...} } class S extends C { int a = 77; static int q() { super.q() + 1;...} int p() {... super.p() + 1;...} }

Notes Static methods, static fields and instance fields are statically bound, while instance methods are dynamically bound. Static methods, static fields, and instance fields can be hidden, while instance methods can be overridden. Hidden members of a subclass instance can be accessed outside the class (text) using static cast and within the subclass (text) using super prefix. cs3180 (Prasad)L8Packages12

Defining a Method with the Same Signature as a Superclass's Method Superclass Instance Method Superclass Static Method Subclass Instance Method Overrides Generates a compile-time error Subclass Static Method Generates a compile-time error Hides cs3180 (Prasad)L8Packages13

Accessibility and Overriding A method can be overridden in a subclass only if the method in the superclass is accessible. If a method in the superclass is not accessible then method in the subclass does not override it even if they have the same signature. cs3180 (Prasad)L8Packages14

package P1; public abstract class AbstractBase { private void pri() { System.out.print("AbstractBase.pri()"); } void pac() { System.out.("AbstractBase.pac()"); } protected void pro() { System.out.("AbstractBase.pro()"); } public void pub() { System.out.("AbstractBase.pub()"); } public final void show() { pri(); pac(); pro(); pub(); }} cs3180 (Prasad)L8Packages15

package P2; // Different package from AbstractBase import P1.AbstractBase; public class Concrete1 extends AbstractBase { public void pri() { print("Concrete1.pri()"); } public void pac() { print("Concrete1.pac()"); } public void pro() { print("Concrete1.pro()"); } public void pub() { print("Concrete1.pub()"); } } new Concrete1().show(); // only protected and public can be overridden OUTPUT: AbstractBase.pri() AbstractBase.pac() Concrete1.pro() Concrete1.pub() cs3180 (Prasad)L8Packages16

package P1; // Same package as AbstractBase import P2.Concrete1; public class Concrete2 extends Concrete1 { public void pri() { print("Concrete2.pri()"); } public void pac() { print("Concrete2.pac()"); } public void pro() { print("Concrete2.pro()"); } public void pub() { print("Concrete2.pub()"); } } new Concrete2().show(); // default (package), protected and public can be overridden OUTPUT: AbstractBase.pri() Concrete2.pac() Concrete2.pro() Concrete2.pub() cs3180 (Prasad)L8Packages17

package P3; // Different package from AbstractBase import P1.Concrete2; public class Concrete3 extends Concrete2 { public void pri() { print("Concrete3.pri()"); } public void pac() { print("Concrete3.pac()"); } public void pro() { print("Concrete3.pro()"); } public void pub() { print("Concrete3.pub()"); } } new Concrete3().show(); // Concrete3.pac() overriddes Concrete2.pac() // which in turn overrides AbstractBase.pac() OUTPUT: AbstractBase.pri() Concrete3.pac() Concrete3.pro() Concrete3.pub() cs3180 (Prasad)L8Packages18

cs3180 (Prasad)L8Packages19 Scope and Lifetime The scope of an identifier declaration is the region of text in which the declaration is effective. E.g., class scope, block scope, global, etc. The lifetime of a variable (or an object) bound to an identifier is the period during which the variable exists in the memory. heapE.g., Static variables and objects allocated via new on heap have “infinite” lifetime. (garbage collection) stack.E.g., Formal parameters and local variables of a method are allocated on stack. (LIFO discipline)

cs3180 (Prasad)L8Packages20 Enumeration myEnumerate(final Object[] arr) { class E implements Enumeration { int count = 0; public boolean hasMoreElements() { return count < arr.length; } public Object nextElement() { return arr[count++]; } } return new E(); } –Scope of arr is the body of myEnumerate function. –Lifetime of the array instance referenced by arr is “infinite”. It survives as long as the Enumeration object returned is “alive”.

cs3180 (Prasad)L8Packages21 Java Development Kit (1995) java.lang (contains Threads ) java.io (contains StreamTokenizer ) java.awt (Abstract Windowing ToolKit) java.net (Support for TCP/IP, HTTP) java.applet (To run code via Browser) java.util (Standard Utilities) javac, java, javadoc

cs3180 (Prasad)L8Packages22

cs3180 (Prasad)L8Packages23