*An nested class is a class that is defined inside another class.

Similar documents
Packages Inner Classes

Topic 6: Inner Classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

C30b: Inner Class, Anonymous Class, and Lambda Expression

Another IS-A Relationship

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Object Orientation Fourth Story. Bok, Jong Soon

Class, Variable, Constructor, Object, Method Questions

CITS2210 Object-Oriented Programming. Topic 10. Java: Nested and Inner Classes

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

And Even More and More C++ Fundamentals of Computer Science

Swing: Building GUIs in Java

Array. Prepared By - Rifat Shahriyar

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Swing: Building GUIs in Java

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CMSC 132: Object-Oriented Programming II

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Timing for Interfaces and Abstract Classes

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Interfaces. An interface forms a contract between the object and the outside world.

Interfaces (1/2) An interface forms a contract between the object and the outside world.

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Introduction to Java. Handout-1d. cs402 - Spring

SRI VIDYA COLLEGE OF ENGG & TECH UNIT -2 INHERITANCE AND INTERFACES SVCET

What about Object-Oriented Languages?

List ADT. Announcements. The List interface. Implementing the List ADT

Character Stream : It provides a convenient means for handling input and output of characters.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

The Essence of OOP using Java, Nested Top-Level Classes. Preface

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

Programming overview


JAVA Programming Language Homework I - OO concept

Midterm Exam CS 251, Intermediate Programming October 8, 2014

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Introduction to Programming Using Java (98-388)

Declarations and Access Control SCJP tips

1 Shyam sir JAVA Notes

Programming Languages and Techniques (CIS120)

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Java: introduction to object-oriented features

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Introduction to Functional Programming in Java 8

Lecture 02, Fall 2018 Friday September 7

Building custom components IAT351

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Arrays. CSE 142, Summer 2002 Computer Programming 1.

Repetition with for loops

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CMSC 132: Object-Oriented Programming II

Programming Languages and Techniques (CIS120)

Principles of Object Oriented Programming. Lecture 4

2IP15 Programming Methods

Lecture 5: Methods CS2301

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Anatomy of a Class Encapsulation Anatomy of a Method

Object Oriented Programming in C#

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Chapter 7 User-Defined Methods. Chapter Objectives

Today's Topics. CISC 458 Winter J.R. Cordy

Fundamental Concepts and Definitions

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Introduction To C#.NET

Chapter 13. Interfaces and Inner Classes

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

( &% class MyClass { }

C08: Inheritance and Polymorphism

Preview from Notesale.co.uk Page 3 of 36

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Chapter 15 Event-Driven Programming and Animations

1.00 Lecture 8. Using An Existing Class, cont.

Overview of Eclipse Lectures. Module Road Map

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

The static keyword Used to denote fields and methods that belong to a class (but not to any particular object).

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

class objects instances Fields Constructors Methods static

PROGRAMMING FUNDAMENTALS

Access Restrictions of Methods/Fields

Contents. 8-1 Copyright (c) N. Afshartous

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Computer Programming: C++

Object Oriented Software Design

Programming by Delegation

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Encapsulation. Chapter Overview. Objectives of this Chapter. Design, Abstraction, and Encapsulation. Interactive Programming In Java Page 1

C10: Garbage Collection and Constructors

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Transcription:

An nested class is a class that is defined inside another class. In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.

Following is the syntax to write a nested class. Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class. class Outer_Demo { class Nested_Demo {

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.

Inner classes are of three types depending on how and where you define them. They are Inner Class Method-local Inner Class Anonymous Inner Class

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

class Outer_Demo { int num; // inner class private class Inner_Demo { public void print() { System.out.println("This is an inner class"); // Accessing he inner class from the method within void display_inner() { Inner_Demo inner = new Inner_Demo(); inner.print();

public class My_class { public static void main(string args[]) { // Instantiating the outer class Outer_Demo outer = new Outer_Demo(); // Accessing the display_inner() method. outer.display_inner(); Here you can observe that Outer_Demo is the outer class, Inner_Demo is the inner class, display_inner() is the method inside which we are instantiating the inner class, and this method is invoked from the main method. If you compile and execute the above program, you will get the following result Output This is an inner class.

class Outer_Demo { // private variable of the outer class private int num = 175; // inner class public class Inner_Demo { public int getnum() { System.out.println("This is the getnum method of the inner class"); return num;

//Rules for instantiation : public class My_class2 { public static void main(string args[]) { // Instantiating the outer class Outer_Demo outer = new Outer_Demo(); // Instantiating the inner class Outer_Demo.Inner_Demo inner = outer.new Inner_Demo(); System.out.println(inner.getNum());

Note that inner class can access outer class instance variables (even private ones). It does this using the object reference <OuterClassName>.this if instance variable name is same as that of outer class Refer to public inner class as <OuterClassName>.<InnerClassName>

Most typically used when inner class is instantiated from outer class. If classes naturally belong together, it is cumbersome to pass a this pointer to a separate outer class just so second class can access first class s properties/methods. Note that inner class can access outer class s private data, making them even more powerful than mechanism implied above!

Inner classes may also be defined within class methods. These are called local inner classes. Principle advantage is scoping: such classes are completely inaccessible anywhere but can be instantiated only within the method where the inner class is defined. Thus, they have no visibility attribute (public, etc.) Also, can NOT access local variables other than those declared with final attribute i.e can access only final variables.

public class Outerclass { // instance method of the outer class void my_method() { int num = 23; // method-local inner class class MethodInner_Demo { public void print() { System.out.println("This is method inner class "+num); // end of inner class // Accessing the inner class MethodInner_Demo inner = new MethodInner_Demo(); inner.print();

public static void main(string args[]) { Outerclass outer = new Outerclass(); outer.my_method(); If you compile and execute the above program, you will get the following result Output This is method inner class 23

An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used whenever you need to override the method of a class or an interface. Also this is very convenient when you want to use a class only once and the code that it contains is concise.

AnonymousInner an_inner = new AnonymousInner() { public void my_method() {...... ;

The following program shows how to override the method of a class using anonymous inner class. abstract class AnonymousInner { public abstract void mymethod(); public class Outer_class { public static void main(string args[]) { AnonymousInner inner = new AnonymousInner() { public void mymethod() { System.out.println("This is an example of anonymous inner class"); ; inner.mymethod();

A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

The syntax of static nested class is as follows class MyOuter { static class Nested_Demo {

public class Outer { static class Nested_Demo { public void my_method() { System.out.println("This is my nested class"); public static void main(string args[]) { Outer.Nested_Demo nested = new Outer.Nested_Demo(); nested.my_method();