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.

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.
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.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Java & Inner Classes L. Grewe. Kinds of of Classes Top level classes Top level classes Declared inside packageDeclared inside package Visible throughout.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CS102--Object Oriented Programming Lecture 16: – Inner classes + review Copyright © 2008 Xiaoyan Li.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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.
CMSC 202 Inner Classes. Aug 7, Simple Uses of Inner Classes Inner classes are classes defined within other classes  The class that includes the.
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.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
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.
Chapter 15 Event-Driven Programming and Animations
Chapter 8 Classes and Objects
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Android Programming Lecture 2
TO COMPLETE THE FOLLOWING:
Overloading and Overriding
Classes & Objects: Examples
Inner Classes 29-Nov-18.
Footnote To Trees: Inner Classes
Inner Classes.
CMSC 202 Inner Classes.
class PrintOnetoTen { public static void main(String args[]) {
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.
Chapter 15 Event-Driven Programming and Animations Part 1
Footnote To Trees: Inner Classes
Inner Classes.
Inner Classes 25-Oct-19.
Presentation transcript:

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 most common – used in graphical programming – local rarely if every used

Member inner class member inner class – nested within another class – given a specific name – attributes and behaviors of nested class accessible to nesting class – attributes and behaviors of nesting class accessible to nested class – compiling creates additional class file outerclassname$innerclassname.class

Member inner class example //Example of Member Inner Class public class OuterClass { private InnerClass ic; public OuterClass () { ic = new InnerClass (); System.out.println("In OuterClass constructor"); } public static void main (String [] args) { OuterClass oc = new OuterClass(); oc.useInnerClass(); } public void useInnerClass() { ic.doSomething(); } private class InnerClass { private InnerClass () { System.out.println("In InnerClass constructor"); } private void doSomething() { System.out.println("I am doing something"); }

Anonymous inner class – nested within another class – not given a specific name – attributes and behaviors of classes are accessible to each other – used almost exclusively for tying events to widgets – compiling creates additional class file outerclassname$1.class (or 2, 3, etc.)

Anonymous inner class example //Example of Anonymous Inner Class class OuterClassWithAnonymousInner { public OuterClassWithAnonymousInner () { //Instantiation of the anonymous inner class // //The anonymous inner class is a subclass of InnerClass (see next slide) (new InnerClass () { public void doSomething () { System.out.println("I am doing something"); } }).doSomething(); System.out.println("In OuterClassWithAnonymousInner constructor"); } public static void main (String [] args) { new OuterClassWithAnonymousInner(); }

Anonymous inner class example continued from previous slide class InnerClass { InnerClass () { System.out.println("In InnerClass constructor"); }