Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.

Slides:



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

8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
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.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Nested Classes CompSci 230 S Software Construction.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Object Oriented Programming in Java Habib Rostami Lecture 10.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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
Chapter 8 Classes and Objects
CompSci 230 S Programming Techniques
Nested class.
User-Defined Classes and ADTs
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Overloading and Overriding
Inner Classes 29-Nov-18.
User-Defined Classes and ADTs
Chapter 8 Classes User-Defined Classes and ADTs
Method of Classes Chapter 7, page 155 Lecture /4/6.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
Inner Classes 25-Oct-19.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Advanced Java class Nested Classes & Interfaces

Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local named anonymous

Some Definitions Nested = a class or interface definition is somewhere inside another one Top-level class or interface = an instance of a type does not need to be instantiated with a reference to any enclosing instance Inner class = an instance of a class does need to be instantiated with an enclosing instance Inner Member class = defined inside another class, but not inside any methods. Inner Local class = defined inside a method Named Inner Local class = has a class name Anonymous Inner Local class = does not have a class name

Top-level Classes & Interfaces Declared inside a class (or interface) definition Must be declared as static The class (or interface) definition is not a member of an instantiation of the outer class (or interface). Shows a close functional relationship between the types. Class referencing syntax: OuterClass.InnerClass You can nest classes in interfaces, and vice versa.

Here Property is a Top-Level Nested Class public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} static public class Property {...} } in another class, far, far away... PropertyValueFinder.Property p = new PropertyValueFinder.Property("hi", String.class); No enclosing instance of PropertyValueFinder is needed to make an instance of Property

Scope of Top-level Nested Classes Enclosed classes share private access with other enclosed classes in the same enclosing class, and with the enclosing class. –(Note: the book is wrong on this point, as shown by the compilation and running of the following example.)

public class TopLevelNested { private int myIntTLN = 3; void test() { new Other1().printOther2(); new Other2().printOther1(); } private void test2() {System.out.println("test 2");} static class Other1 { private int myIntO1 = 1; static void printOther2(){ System.out.println(new Other2().myIntO2); new TopLevelNested().test2(); } private static class Other2 { private int myIntO2 = 2; private static void printOther1(){ System.out.println(new Other1().myIntO1); System.out.println(new TopLevelNested().myIntTLN); }

Inner Classes Member –Defined in class definition, not static Local (private, not static) –Defined in method definition –May be named or anonymous

Using Inner Classes Reasons to use: –Nice for classes that implement 1-method interfaces –Adapter classes (see ex. page. 129) Presenting some aspect(s) of the enclosing class in terms of an interface that it does not implement Can not be static. The class definition is a member of the outer class. (And therefore accessible via reflection.) Inner object shares scope of outer object. These are classes, not interfaces.

Here Property is a Member Inner Class public class PropertyValueFinder { final String GET = "get"; public void printProperties(Object o) {... } private Property propertyGottenBy(Method m) {...} private boolean isGetter(Method m) {...} public class Property {...} } in another class, far, far away... PropertyValueFinder pvf = new PropertyValueFinder(); PropertyValueFinder.Property p = pvf.new Property("hi", String.class); An enclosing instance of PropertyValueFinder is needed to make an instance of Property

Scope of Inner Classes Scope: –in the inner class, “this” refers to both the instance of the inner class and its enclosing class instance –this means that the inner object can refer directly to fields and methods of the outer object –See example next page

public class Outer { private String myString = "outer"; private String outerString ="outer outer !"; void test() { System.out.print("test "); System.out.println(myString); new Inner().testInner(); } private void test2() { System.out.print("test2 "); System.out.println(myString); } class Inner { private String myString = "inner"; private void testInner(){ System.out.print("testInner "); System.out.println(myString); System.out.println(outerString); test2(); }

Inner classes extend other classes Inheritance vs. Enclosing Scope –If not fully specified, order is: inner, super, enclosing (book says won’t compile, but I don’t find this true) –If all of these exist, you should specify to: inner: this.X super: super.X enclosing: EnclosingClassName.this.X

Enclosing Class has outer most scope public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { //String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: outer

Super class has scope between Enclosing and Inner class public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { //String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: super

Inner class has innermost scope public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(s); } class SuperClass { String s = "super"; } in another class: ScopeConflict sc = new ScopeConflict(); ScopeConflict.Inner inner = sc.new Inner(); inner.foo(); output: inner

Specifying which scope you want: public class ScopeConflict { String s = "outer"; class Inner extends SuperClass { String s = "inner"; void foo(){ System.out.println(this.s); System.out.println(super.s); System.out.println(ScopeConflict.this.s); } class SuperClass { String s = "super"; } output: inner (from this.s) super (from super.s) outer (from ScopeConflict.this.s)

Member Inner Class as Superclasses public class Computer { int model; Computer(int i) { model = i; } public class HardDrive { int size; public HardDrive(int i) { size = i; } public HardDrive() { size = 40; } class SCSI extends Computer.HardDrive{ SCSI (Computer c) { c.super(80); }

Local Inner Classes Defined inside a method in an enclosing class Has access not only to the scope of the enclosing object(s), but also to the scope of the enclosing method and any code blocks that are final. Two types –Local Inner Named Classes –Local Inner Anonymous Classes

Local Inner Classes In a instance method –Can use all members of the enclosing instance In a class method –Can only use class members (because it’s in a static context).

Local Inner Classes Why must the local enclosing fields be final to be in scope? Because the objects (not the classes) can be used outside of the scope they were created in, so they must have a copy of the local fields from their original scope. These local copies would be wrong if the fields changed, so the fields must be final to protect the integrity of the local inner class object.

Named Local Inner classes public class Outer { public void outerMethod() { class InnerClass {...} } See example 2-30 on page 129 –accesses all of these things from different scopes: private instance variable of enclosing object [equationInput] arguments of the instance method the local inner class was defined in [input1 & input2] private variable local to method [localVar[2]] private instance of local inner class [normalField]

Anonymous Local Inner Classes syntax: –Type objectName = new [SuperType()] {...} (If you don’t provide a SuperType, defaults to Object, as usual.) This syntax both defines the class and instantiates it. No name, and no constructors. To initialize instance variables, use a initialization statement: { // initializations go here }

Group Task None for now! Will be combined with other tasks, relating to Threads, Events, GUI, etc.