Nested References 2 inner reference data types Classes-Interfaces.

Slides:



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

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
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.
Software Engineering Implementation Lecture 3 ASPI8-4 Anders P. Ravn, Feb 2004.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Lecture 2: Object Oriented Programming I
Nested Classes Yoshi Modified from:
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Access to Names Namespaces, Scopes, Access privileges.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Principles of Object-Oriented Software Development The language Java.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
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 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Programming in Java CSCI-2220 Object Oriented Programming.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
2.3 The Java API Documentation. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Inner Classes
Nested Classes CompSci 230 S Software Construction.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Object-Oriented Programming: Polymorphism Chapter 10.
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.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
CompSci 230 S Software Construction
Chapter 8 Classes and Objects
University of Central Florida COP 3330 Object Oriented Programming
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
An Introduction to Java – Part I
Threads Chate Patanothai.
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
null, true, and false are also reserved.
Overloading and Overriding
Introduction to Java Programming
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Unit 1 Lab16.
Namespaces, Scopes, Access privileges
Events, Event Handlers, and Threads
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Chapter 5 Classes.
Presentation transcript:

Nested References 2 inner reference data types Classes-Interfaces

What are the different Nested Ref. Data Types? Nested (inner) classes Nested (inner) Interfaces

What is a nested reference? A reference contained by another reference A reference contained by a method A reference with no name ->anonymous anonymous class.

Why do I need a nested reference? To create a temporary reference To isolate the reference name space To create a new reference organization To make use of new object-oriented designs To create a local reference Simplify interfaces (façade design pattern).

What are the different types of Nested classes? nested static class nested dynamic class

Nested static classes Can be instanced without instancing the containing class. Class Point3d{ static class Float(){…} static class Double(){…} }

What kinds of access can I have for a static class? public – public ref organization default – package ref organization private – yes, you can have a private inner class! protected – accessible to subclasses of the outer class

A static inner class public abstract class Point3d { public static class Float extends Point3d { public Float(float x, float y, float z) { … } public static class Double extends Point3d { public Double (double x, double y, double z) {...} } Point3d pf = new Point3d.Float(10,20,30); }

Inner class instead of packages! class java { class awt { class Frame { } class Component { }

Making an Anonymous Inner class interface Runnable { public void run(); } Thread t = new Thread( new Runnable() { public void run() { doStuffHere(); } });

What can you build an anonymous class from? an interface. an abstract class. non-abstract class.

Example of an anonymous inner class from a non-abstract class. class Customer { private String name = “j doe”; public String toString() { return name;} } class Payroll { Customer c = new Customer(){public String toString() {return “hello world”;} }

Example of an named inner class from a non-abstract class. class Customer { private String name = “j doe”; public String toString() { return name;} } class Payroll { class Customer2 extends Customer{ public String toString() {return “hello world”;} }

An anonymous Member class is sometimes called an anonymous class class OuterClass { class namedMember { } Runnable r = new Runnable() { public void run() { // do stuff here }

Nomenclature All anonymous classes are inner classes. Anonymous inner class = anonymous class. Convention is to call it an anonymous inner class

Why not use a named class? class Job implements Runnable { public void run() { do Stuff Here... } Thread t = new Thread(new Job()); Ans: you only needed one new job. Like singleton

Make a 1000 classes.... Local anonymous inner class public static void main(String args[]){ for (int i=0; i < 1000; i++) new Foo(new Runnable() { public void run() { This a unique class..... } }); }

Nomenclature Few people say: local anonymous inner class. Typically call it anonymous inner class

Command Pattern A design pattern Have commands reside in the instance, so the instance knows how to run itself. new Job(2) { public void doCommand() {...} }

PrintFunction class Print { public static void printFunction(Fcn fcn) { for (int x=0; x < 100; x++) System.out.println(fcn.f(x)); }

Fcn, The Interface public interface Fcn { double f(double x); } print.printFunction(new Fcn() { double f(double x) { return x*x; } });

RunButton new RunButton(“ok”) { public void run() { doesTheOKThing(); }

Using the Interface...for commands class Commando { public Runnable getRunnable() { // factory pattern. return new Runnable() { // anonymous local class public void run() { System.out.println(“wow”); } // Commando c = new Commando(); } // Runnable r = c.getRunnable(); }

How do I use Commando? Commando c = new Commando(); Thread t = new Thread(c.getRunnable()); t.start(); //call back on the run method

Local inner class A class defined inside of a method Not accessible to outer classes Or the other methods in the same class!

Why do I need local inner classes? Factory pattern with an anonymous inner class! For example, a local anonymous inner class!!

This is pretty common public Runnable getRunnable() { new Runnable() { public void run() { System.out.println(“wow”); }

Named Local Inner class class RunnableThings { Runnable getARunThing() { class Foo extends Job{} class Job implements Runnable { public void run(){} } return new Job(); }

Nested Interface public class Roach { interface killable { boolean isAlive(); } class Colony implements killable { boolean isAlive() {return true;} }

Summary two kinds of nest ref data types: interface local inner interface not permitted! member inner interface class local inner class (named or anonymous) member inner class (named or anonymous)