CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University February 18, 2008 / Lecture 5
Outline Course Status Course Information & Schedule Assignments Basics.this and.new Anonymous Nested Classes Other Topics
Course Information Instructor Andy Mroczkowski uamroczk@cs.drexel.edu Office Location: UC 147 Office Hours: Monday 6-7 & evenings by appointment Course CS 190 - Java Web: www.cs.drexel.edu/ uamroczk/cs190 Book: Thinking in Java, 4th Edition Previous edition available for free at wwww.mindview.net/books/tij/ Course materials adapted from Nadya Belov
Schedule 1 Jan 7 Jan 14 Jan 21 Jan 28 Feb 4 Feb 11 Feb 18 Feb 25 Mar 3 Mar 10 Mar 17 Course Overview, Introduction to Java No Class No Class (University Holiday) Object Oriented Concepts, Control Statements Access Control, Intialization & Cleanup, Reuse, Interfaces, Containers, Exceptions, Polymorphism, Midterm Review Midterm, Type Information, Generics, Arrays File I/O, Threading GUI, Special Topics (Networking, Reflection) Final 1 Also subject to change
Assignment 4 Questions? Due 22 Feb 11:59 PM EST
Assignment 4 Questions? Due 22 Feb 11:59 PM EST
Basics A class that is defined within a certain scope. Example class O u t t i e { class I n n i e {
Basics Example class O u t t i e { class I n n i e { class MoreInnie {
Parcel1 Example public class Parcel1 { class Contents { class D e s t i n a t i o n { private S t r i n g label ; D e s t i n a t i o n ( S t r i n g whereto ) { label = whereto ; S t r i n g readlabel ( ) { return label ; public void ship ( S t r i n g dest ) { Contents c = new Contents ( ) ; D e s t i n a t i o n d = new D e s t i n a t i o n ( dest ) ; System. out. p r i n t l n ( d. readlabel ( ) ) ; public s t a t i c void main ( String [ ] args ) { Parcel1 p = new Parcel1 ( ) ; p. ship ( " Tasmania " ) ;
References to public class O u t t i e { class I n n i e { Innie myinnie = new Innie ( ) ; Innie getinnie ( ) { return myinnie ; public s t a t i c void main ( String [ ] args ) { Outtie out = new Outtie ( ) ; O u t t i e. I n n i e i n = out. g e t I n n i e ( ) ;
Is that it?
Accessing the outer-class Example class O u t t i e { S t r i n g s = " Hi " ; class I n n i e { S t r i n g t = s ;
Accessing the outer-class Example class O u t t i e { private S t r i n g s = " Hi " ; class I n n i e { S t r i n g t = s ; Even private members
Example Accessing the outer-class class O u t t i e { private S t r i n g s = " Hi " ; void boom ( ) { class I n n i e { S t r i n g t = s ; void bang ( ) { boom ( ) ; And methods too!
Selector Example interface S e l e c t o r { boolean end ( ) ; Object c u r r e n t ( ) ; void next ( ) ;
Selector Example (Cont.) public class Sequence { private Object [ ] items ; private i n t next = 0; public Sequence ( i n t size ) { items = new Object [ size ] public void add ( Object x ) { i f ( next < items. length ) { items [ next ++] = x ; private class SequenceSelector implements S e lector { private i n t i = 0; public boolean end ( ) { return i == items. length ; public Object c u r r e n t ( ) { return items [ i ] ; public void next ( ) { i f ( i < items. length ) i ++; public Selector selector ( ) { return new SequenceSelector ( ) ;
Selector Example (Cont.) public s t a t i c void main ( String [ ] args ) { Sequence sequence = new Sequence ( 10); for ( i n t i = 0; i < 10; i ++ ) sequence. add ( I n t e g e r. t o S t r i n g ( i ) ) ; Selector selector = sequence. selector ( ) ; while (! s e l e c t o r. end ( ) ) { System. out. p r i n t ( s e l e c t o r. c u r r e n t ( ) + " " ) ; s e l e c t o r. next ( ) ;
Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this
Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this
Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this
Accessing the outer-class Accessing outer-class s instance variables? Direct access Accessing outer-class instance itself?.this
Accessing the outer-class Example class O u t t i e { class I n n i e { O u t t i e g e t O u t t i e ( ) { return O u t t i e. this ;
Example Creating an Inner-Class directly class O u t t i e { class I n n i e { private i n t y = 10; public gety ( ) { return y ; public s t a t i c void main ( S t r i n g [ ] args ) { O u t t i e out = new O u t t i e ( ) ; O u t t i e. I n n i e i n = out.new I n n i e ( ) ; System. out. p r i n t l n ( i n. gety ( ) ) ;
Anonymous public class O u t t i e { public I n n i e g e t I n n i e ( ) { return new I n n i e ( ) { private i n t y = 10; public i n t gety ( ) { return y ; ; Madness! public s t a t i c void main ( S t r i n g [ ] args ) { O u t t i e out = new O u t t i e ( ) ; System. out. p r i n t l n ( out. g e t I n n i e ( ). gety (
Anonymous - Why? Shorthand Unclutter the code Common with Threads
Anonymous - Why? Shorthand Unclutter the code Common with Threads
Anonymous - Why? Shorthand Unclutter the code Common with Threads
Anonymous - Why? Shorthand Unclutter the code Common with Threads
Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final
Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final
Anonymous - Limitations Unlike regular classes, can only implement or extend but not both Objects used in the anonymous class but defined outside must be final
Nested Classes Don t need a connection to the outer-class? Make the the inner-class static
Nested Classes Don t need a connection to the outer-class? Make the the inner-class static
Nested Classes Don t need a connection to the outer-class? Make the the inner-class static
Nested Classes Example class O u t t i e { s t a t i c class I n n i e { s t a t i c i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ;
Nested Classes What about? class O u t t i e { s t a t i c class I n n i e { i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ;
Nested Classes What about? class O u t t i e { s t a t i c class I n n i e { i n t x ; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( O u t t i e. I n n i e. x ) ; FAIL
Nested Classes Fixed class O u t t i e { s t a t i c class I n n i e { i n t x = 5; public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ;
Nested Classes - Other Points Nested-classes can have static data and methods, regular inner-classes cannot
Other Topics Classes inside interfaces Inheriting inner-classes Overriding inner-classes Local inner-classes
Classes inside interfaces Example interface Car { void d r i v e ( ) ; class Engine { i n t c y l i n d e r s ;
Inheriting inner-classes Example class O u t t i e { class I n n i e { public class I n h e r i t I n n i e extends O u t t i e. I n n i e { Whats the problem?
Inheriting inner-classes Example - Fixed class O u t t i e { class I n n i e { public class I n h e r i t I n n i e extends O u t t i e. I n n i e { I n h e r i t I n n i e ( O u t t i e o ) { o. super ( ) ;
Overriding inner-classes Example class Egg { private Yolk y ; protected class Yolk { public Yolk ( ) { p r i n t ( " Egg. Yolk ( ) ) p u b l i c Egg ( ) { p r i n t ( "New Egg ( ) " ) ; y = new Yolk ( ) ; p u b l i c class BigEgg extends Egg { p u b l i c class Yolk ( ) { p r i n t ( " BigEgg. Yolk ( ) " ) ; p u b l i c s t a t i c void main ( S t r i n g [ ] args ) { new BigEgg ( ) ;
Overriding inner-classes Output New Egg() Egg.Yolk()
Local inner-classes Example class O u t t i e { void go ( ) { class I n n i e { i n t x = 5; System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ; public s t a t i c void main ( S t r i n g [ ] args ) { (new O u t t i e ( ) ). go ( ) ;
Example Local inner-classes class O u t t i e { void go ( ) { class I n n i e { i n t x = 5; System. out. p r i n t l n ( (new I n n i e ( ) ). x ) ; public s t a t i c void main ( S t r i n g [ ] args ) { (new O u t t i e ( ) ). go ( ) ; Inner-classes can be declared in any scope.