CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.

Slides:



Advertisements
Similar presentations
COSC 2006 Data Structures I Instructor: S. Xu URL:
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
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.
Lecture 10 Documentation, Garbage Collection, and Nested Classes/Interfaces.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Programming in Java CSCI-2220 Object Oriented Programming.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Nested Classes CompSci 230 S Software Construction.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Topics Instance variables, set and get methods Encapsulation
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter 7: Cloning and RTTI
CompSci 230 S Software Construction
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
CSE 413, Autumn 2002 Programming Languages
Chapter 9 Inheritance and Polymorphism
The Building Blocks Classes: Java class library, over 1,800 classes:
Interfaces.
Presentation transcript:

CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private

CS884 (Prasad)java11Extn2 Nested classes StaticStatic Top-level Classes and Interfaces Like ordinary package member except for its name. May use static members of its enclosing class. Member Classes Each instance has an enclosing instance.Each instance has an enclosing instance. May use members of its enclosing classes. Local Classes Defined in a block of code. May also use final local variables and parameters. Anonymous classes One instance only.

CS884 (Prasad)java11Extn3 public class LinkedList { // static by default public interface Linkable { public Linkable getNext(); public void setNext(Linkable node); } Linkable head; public void insert(Linkable node) {...} public void remove(Linkable node) {...} }

CS884 (Prasad)java11Extn4 class LinkableInt implements LinkedList.Linkable { int i; public LinkableInt(int _i){i= _i;} LinkedList.Linkable next; public LinkedList.Linkable getNext() {return next;} public void setNext(LinkedList.Linkable node) {next = node;} }

CS884 (Prasad)java11Extn5 Member Class Every instance of a member class is internally associated with an instance of the enclosing class. The methods of a member class can implicitly refer to the fields defined within the member class, as well as those defined by any enclosing class (including private fields.) static final members permitted.

CS884 (Prasad)java11Extn6 public class BankAccount{ private long number; private int balance; private Action lastAct; public class Action { private String act; private int amount; Action(String a, int amt) { act = number + “:” + a; amount = amt; } public void deposit(int amt) { balance += amt; lastAct = new Action(“deposit”,amt); } public void withdraw(int amt) { balance -= amt; lastAct = new Action(“withdraw”,amt); }

CS884 (Prasad)java11Extn7 public class BankAccount{ private long number; private int balance; private Action lastAct; public class Action { … } public void deposit(int amt) { … } public void withdraw(int amt) { … } public void transfer(BankAccount ac, int amt) { ac.withdraw(amt); deposit(amt); lastAct = new Action(“transfer”,amt); ac.lastAct = ac.new Action(“transfer”,amt); }

CS884 (Prasad)java11Extn8 Scope of Fields public class A { public String name="a"; public class B { public String name="b"; public class C { public String name="c"; public void printNames() {…} } public static void main(String[] a){…} }

CS884 (Prasad)java11Extn9 public void printNames() { // class C System.out.println(name); // c System.out.println(this.name); // c System.out.println(B.this.name); // b System.out.println(A.this.name); // a } public static void main(String[] a) { //class A A a = new A(); B b = a.new B(); A.B.C c = b.new C(); c.printNames(); }

CS884 (Prasad)java11Extn10 javap -p A$B$C public class A.B.C extends java.lang.Object { /* ACC_SUPER bit NOT set */ private final A.B this$1; public java.lang.String name; public A.B.C(A.B); public void printNames(); }

CS884 (Prasad)java11Extn11 javap -p A.B public class A. B extends java.lang.Object { private final A this$0; public java.lang.String name; public A.B(A); static A access$0(A.B); // related to accessing private {...A.B.C...} }

CS884 (Prasad)java11Extn12 Scope vs Inheritance Top-level class can extend a member class. Class hierarchy –superclass to subclass Containment hierarchy –containing class to nested class Whenever there is a name conflict between an inherited member and a member of an enclosing class, Java requires explicit disambiguation. The name of a member class must differ from the name of the enclosing package or class.

CS884 (Prasad)java11Extn13 class A extends A.B { int i = 0; class B { int i = 1; } Pathological cases Cyclic Inheritance: illegal Static subclass of a member class: legal class A { int i = 0; class B { int i = 1; } static class C extends B { int i = 2; C(A a) { a.super(); } } public static void main(String[] args) { A a = new A(); B b = a.new B(); C c = new C(a); }

CS884 (Prasad)java11Extn14 Local Class (vs Member class) A local class is similar to a member class except that its methods can also access final local variables, final formal method parameters, and final exception parameters. An instance of a local class has private fields to hold values of final local variables, final exception parameter, and final method parameters, in addition to a private reference to the enclosing class instance.

CS884 (Prasad)java11Extn15 Enumeration Enumeration myEnumerate(final Object[] arr) { Enumeration 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 should be at least as long as the Enumeration object.

CS884 (Prasad)java11Extn16 import java.util.*; class XYZ { int i; Enumeration myEnumerate(final Object[]arr) { class E implements Enumeration { int count = i; public boolean hasMoreElements() { return count < arr.length; } public Object nextElement() { return arr[count++]; } } return new E(); }

CS884 (Prasad)java11Extn17 class XYZ$1$E extends java.lang.Object implements java.util.Enumeration { private final java.lang.Object val$arr[]; int count; XYZ$1$E(java.lang.Object[], XYZ); public boolean hasMoreElements(); public java.lang.Object nextElement(); } javap -p XYZ$1$E

CS884 (Prasad)java11Extn18 public class Weird { // cf. closure interface IntHolder { int getValue(); } public static void main(String[] args) { IntHolder[] holders = new IntHolder[10]; for(int i=0; i<10; i++) { final int fi = i; IntHolder class MyIntHolder implements IntHolder { public int getValue() { return fi; } } holders[i] = new MyIntHolder(); } // MyIntHolder invisible here for(int i=0; i<10; i++) System.out.println(holders[i].getValue()); System.out.println(holders[i].getValue()); }

CS884 (Prasad)java11Extn19 Anonymous Class Local class without a name. Combines class definition and instantiation. No constructor. Can implement an interface (by implicitly extending class Object ). new ( ) { } new ( ) { }

CS884 (Prasad)java11Extn20 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble extends Applet { public void init() { this.addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g = getGraphics(); int x = e.getX(), y = e.getY(); g.drawLine(0, 0, x, y); } } ); }}

CS884 (Prasad)java11Extn21 class LocalVarThreads { public static void main(final String[] args) { for (int i = 0; i < args.length; i++) { final int ii = i; Runnable r = new Runnable() { public int count; public void run() { System.out.println(" > "+count++); }; } new Thread(r).start(); } }}

CS884 (Prasad)java11Extn22 Other new features Instance Initializers similar to class initializers serves as a constructor in anonymous classes “Blank” finals assign only once prior to use instead of in decl. Anonymous arrays int[] a; a = new int[] {1,2,3,4}; Class literals String.class, Integer.class, Integer.TYPE, etc In Java 1.1, class is a keyword and a field.

CS884 (Prasad)java11Extn23 Reflection Introspection Meta-programming

CS884 (Prasad)java11Extn24 What does Reflection API do? Java Runtime Environment maintains an immutable Class object that contains information about a class. Reflection API enables a program to:  Determine the class of an object.  Get information about a class's modifiers, fields, methods, constructors, and superclasses.  Find out what constants and method declarations belong to an interface.

CS884 (Prasad)java11Extn25  Create an instance of a class whose name is not known until runtime.  Get and set the value of an object's field, even if the field name is unknown to your program until runtime.  Invoke a method on an object, even if the method is not known until runtime.  Create a new array, whose size and component type are not known until runtime, and then modify the array's components.

CS884 (Prasad)java11Extn26 Applications: Construction of development tools. Tools that need to discover and use public members of a target object based on its class. –GUI builders selecting and creating a component. determining properties, methods, events, etc supported by a JavaBean. customizing a component to running a method.

CS884 (Prasad)java11Extn27 (cont’d) Tools that need to discover and use members declared in a given class. –Class browsers requires information about classes to display their fields, methods, constructors, etc. –Debuggers accessing and modifying field values. –Interpreters

CS884 (Prasad)java11Extn28 Java Core Reflection APIs package java.lang.reflect new interface: Member new classes: Field, Method, Constructor. new class: Array new utility class: Modifier package java.lang new wrapper classes: Byte, Short Classupdated class: Class Classnew objects: instances of class Class to represent primitive types.