Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Nested References 2 inner reference data types Classes-Interfaces.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
NESTED CLASSES REFLECTION PROXIES.
CompSci 230 S Software Construction
Classes (Part 1) Lecture 3
Lecture 12 Inheritance.
Chapter 8 Classes and Objects
CompSci 230 S Programming Techniques
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Building Java Programs
Inner Classes 29-Nov-18.
CS 302 Week 9 Jim Williams.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 8 Classes User-Defined Classes and ADTs
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Chapter 11 Inheritance and Polymorphism Part 1
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Objects with ArrayLists as Attributes
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Chapter 5 Classes.
Presentation transcript:

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 2 public class Person1 { private Name name; // CONSTRUCTOR public Person1(String firstName, String lastName){ name = new Name(); name.firstName = firstName; name.lastName = lastName; } public String toString(){ return name.firstName + " " + name.lastName; } public String getLastFirstName(){ return name.lastName + ", " + name.firstName; } // INNER CLASS Name class Name { private String firstName; String lastName; } Person1 Inner Class = Name The private instance variable of outer class Person1 is a Name object, which is an inner class of Person1. Outer class has access to inner class members. Inner class members are visible to outer class, regardless of access.

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 3 Creating Person1 objects From OUTSIDE Person1 Same code as on right, as long as Person1 is visible. Note: The inner class Name and its instance variables can have any access. From INSIDE Person1 public class Test1 { public static void main(String[] args){ Person1 gary = new Person1(“John", “Doe"); Person1 abe = new Person1("Abe", "Lincoln"); Set s = new HashSet(); s.add(gary); s.add(abe); Iterator it = s.iterator(); Person1 p; while(it.hasNext()){ p = (Person1)it.next(); System.out.println(p); }

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 4 public class Person2 { private Name name; // INNER CLASS Name class Name { private String firstName; String lastName; public Name(String firstName, String lastName){ this.firstName = firstName; this.lastName = lastName; } public String toString(){ return firstName + " " + lastName; } public String getLastFirstName(){ return lastName + ", " + firstName; } Person2 Inner Class = Name We will create Person2.Name objects from outside the outer class (using Test2.java).

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 5 Creating Person2.Name objects From OUTSIDE Person2 public class Test2 { public static void main(String[] args){ // CREATE NAME OBJECTS, // AND PUT THEM INTO A VECTOR Person2 p2 = new Person2(); Person2.Name georgeWashington = p2.new Name("George", "Wash"); Person2.Name marthaStewart = new Person2().new Name("Martha", "Stewart"); Vector nameVec = new Vector(); nameVec.add(georgeWashington); nameVec.add(marthaStewart); int size = nameVec.size(); for(int i = 0; i < size; i++) System.out.println( nameVec.get(i) ); } COMMENTS The object that we create has static class: Person2.Name But to instantiate it, we must first create an instance of the outer class: Person p2 = new Person2(). Using p2, we can get an instance of the inner class name. The syntax is different – notice the way we call the constructor of the inner class. Person2.Name georgeWashington = p2.new Name(“George”, “Wash”); We can also use an anonymous reference to the outer class: Person2.Name marthaStewart = new Person2().new Name("Martha", "Stewart");

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 6 See Outer Class From Inner Class In order to get a reference to the outer class from the inner class, you must prepend the outer class name to “.this”.

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 7 Inner Class Accessing Outer Class class Outer { private int value = 5; private String name = "Outer class"; // PRINT THE CURRENT // DATE AND TIME void printTheDate(){ System.out.println( new Date().toString()); } class Inner { void printOuterVariables(){ // GET A REFERENCE TO // THE OUTER CLASS Outer outer = Outer.this; System.out.println(outer.value); System.out.println(outer.name); } public class Test3 { public static void main(String[] args){ Outer.Inner oi = new Outer().new Inner(); oi.printOuterVariables(); } It is possible to have the inner class access member variables from the outer class. The inner class must get a reference to outer class. From method printOuterVariables(), you get a reference to the outer class by appending “.this” to the outer class’s name (Outer, in this case): Outer outer = Outer.this With that reference to the outer class, you can access anything in the outer class.

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 8 Creating Classes Inside Methods public class Test4 { public static void main(String[] args){ new Outer2().doStuff(); } class Outer2 { private String x = "outer"; void doStuff(){ class MyInner { public void seeOuter() { System.out.println( "Outer x = " + x); } } // end class MyInner MyInner mi = new MyInner(); mi.seeOuter(); } Notice that class MyInner is defined witin the method doStuff(). A reference to that class is created in doStuf() after the class is defined. MyInner mi = new MyInner(); The reference to that class (mi) is used to execute the MyInner method seeOuter(). Method seeOuter() has access to the instance variables of the enclosing class (Outer2).

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 9 Anonymous Inner Classes class Popcorn { public void pop(){ System.out.println(“popcorn”) } // CREATE A SUBCLASS OF POPCORN // WITHIN FOOD class Food { Popcorn p = new Popcorn() { public void pop() { System.out.println( “anonymous popcorn”); } }; // Note the semicolon after class def. } An anonymous subclass of Popcorn is created within class Food. main() creates a Food object, and uses it to access the instance of the Popcorn subclass. public class X { public static void main(String[] args){ Popcorn p = new Popcorn(); p.pop(); // This time, no ref. to object new Popcorn().p.pop(); }

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 10 Anonymous Inner Classes An anonymous subclass of Cup is created within class Dish. main() creates a Dish object, and uses it to access the instance of the Cup subclass. Cup is a class, in this example. Later, we will change it to an interface. class Cup { protected int percentFilled; protected boolean isEmpty(){ if(percentFilled == 0) return true; return false; } protected int getPercentFilled(){ return percentFilled; } public String toString(){ return "percent filled = " + percentFilled; } public class CupTest1 { public static void main(String[] args){ Dish dish = new Dish(); System.out.println( dish.cup.toString() ); } // CREATE A SUBCLASS OF CUP // WITHIN NAME class Dish { Cup cup = new Cup(){ String color; public String toString(){ return "From subclass: " + super.toString(); } }; // Note the semicolon after class def. }

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 11 Anonymous Inner Classes interface Popcorns { public void pop(){ System.out.println(“popcorn”) } // CREATE A SUBCLASS OF POPCORN // WITHIN FOOD class Food { Popcorn p = new Popcorn() { public void pop() { System.out.println( “anonymous popcorn”); } }; // Note the semicolon after class def. } Popcorns is an interface this time. So, the anonymous class created in Foods implements Popcorns. main() creates a Dish object, and uses it to access the instance of the Cup subclass. public class X { public static void main(String[] args){ Food food = new Food(); System.out.println( food ); }

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 12 Anonymous Inner Classes An anonymous subclass of Cups is created within class Dish. main() creates a Dish object, and uses it to access the instance of the Cup subclass. Notice that Cups is an interface, so what does that say about the members of Cups? What modifiers are implicit? interface Cups { int percentFilled = 25; boolean isEmpty(); int getPercentFilled(); public String toString(); } public class CupTest2 { public static void main(String[] args){ Dishes dish = new Dishes(); System.out.println( dish.cup.toString() ); } // WHAT IS WRONG WITH THIS CODE? class Dishes { Cups cup = new Cups(){ boolean isEmpty(){ if(percentFilled == 0) return true; return false; } int getPercentFilled(){ return percentFilled; } String toString(){ return "percent filled = " + percentFilled; } }; // Note the semicolon after the class def. }

Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 13 End of Chapter 8