JAVA - INNER CLASSES



Similar documents
JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

LAB4 Making Classes and Objects

Java Interview Questions and Answers

Introduction to Object-Oriented Programming

JAVA - OBJECT & CLASSES

Basic Object-Oriented Programming in Java

J a v a Quiz (Unit 3, Test 0 Practice)

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

C++ INTERVIEW QUESTIONS

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Install Java Development Kit (JDK) 1.8

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Example of a Java program

Java from a C perspective. Plan

The different kinds of variables in a Java program

JAVA - MULTITHREADING

Conversion Constructors

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Fundamentals of Java Programming

Solutions to Quick Check Questions. Defining Your Own Classes Part 1

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

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

Lecture 5: Java Fundamentals III

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

VB.NET - CLASSES & OBJECTS

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

GUI Event-Driven Programming

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Object-Oriented Programming in Java

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

C++FA 5.1 PRACTICE MID-TERM EXAM

Introduction to Java

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Java Basics: Data Types, Variables, and Loops

Building Java Programs

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Introduction to Programming

Java Crash Course Part I

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

Lab Experience 17. Programming Language Translation

Object Oriented Software Design

Table of Contents. Java CGI HOWTO

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Lecture 22: C Programming 4 Embedded Systems

Chapter 13 - Inheritance

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

VHDL Test Bench Tutorial

JAVA COLLECTIONS FRAMEWORK

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

OpenCL Static C++ Kernel Language Extension

Object Oriented Software Design

Keil C51 Cross Compiler

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Introduction to Data Structures

The Google Web Toolkit (GWT): Declarative Layout with UiBinder Basics

AP Computer Science Java Subset

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Chapter 2: Elements of Java

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

COMP 110 Prasun Dewan 1

Moving from CS 61A Scheme to CS 61B Java

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

Java Programming Fundamentals

CS 106 Introduction to Computer Science I

16 Collection Classes

Lecture 9. Semantic Analysis Scoping and Symbol Table

Building Java Programs

Advanced OOP Concepts in Java

Pemrograman Dasar. Basic Elements Of Java

Chapter 5 Names, Bindings, Type Checking, and Scopes

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Abstract Class & Java Interface

Object Oriented Software Design II

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Using Files as Input/Output in Java 5.0 Applications

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Plugin JUnit. Contents. Mikaël Marche. November 18, 2005

Object-Oriented Programming Lecture 2: Classes and Objects

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

Paillier Threshold Encryption Toolbox

Transcription:

http://www.tutorialspoint.com/java/java_innerclasses.htm JAVA - INNER CLASSES Copyright tutorialspoint.com Nested Classes 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. Syntax The syntax to write a nested class is given below. Here the class Outer_Demo is the outer class and the class Inner_Demo is the nested class. class Outer_Demo{ class Nested_Demo{ Nested classes are divided into two types: Non-static nested classes: These are the non-static members of a class. Static nested classes: These are the static members of a class. Inner Classes Non staticnestedclasses 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 Classlass Anonymous Inner Class 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. Given below is the program to create an inner class and access it. In the given example, we make the inner class private and access the class through a method. 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{ //Instantiating the outer class Outer_Demo outer=new Outer_Dem(); //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. This is an inner class. Accessing the Private Members As mentioned earlier, inner classes are also used to access the private members of a class. Suppose a class is having private members to access them. Write an inner class in it, return the private members from a method within the inner class, say, getvalue, and finally from another class fromwhichyouwanttoaccesstheprivatemembers call the getvalue method of the inner class. To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the object of the outer class, you can instantiate the inner class as shown below. Outer_Demo outer=new Outer_Demo(); Outer_Demo.Inner_Demo inner=outer.new Inner_Demo(); The following program shows how to access the private members of a class using 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;

public class My_class2{ //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()); The value of num in the class Test is: 175 Method-local Inner Class In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class. 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(); Outerclass outer =new Outerclass(); outer.my_method(); This is method inner class Anonymous Inner Class 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. The syntax of an anonymous inner class is as follows: 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 { AnonymousInner inner= new AnonymousInner(){ public void mymethod(){ System.out.println("This is an example of anonymous inner class"); ; inner.mymethod(); This is an example of anonymous inner class In the same way, you can override the methods of the concrete class as well as the interface using an anonymous inner class. Anonymous Inner Class as Argument Generally if a method accepts an object of an interface, an abstract class, or a concrete class, then we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, then we can directly pass it to the method. But in all the three cases, you can pass an anonymous inner class to the method. Here is the syntax of passing an anonymous inner class as a method argument: obj.my_method(new My_Class(){ public void Do(){...... ); The following program shows how to pass an anonymous inner class as a method argument. //interface interface Message{ String greet(); public class My_class { //method which accepts the object of interface Message public void displaymessage(message m){ System.out.println(m.greet() +", This is an example of anonymous inner calss as an argument"); //Instantiating the class My_class obj=new My_class(); //Passing an anonymous inner class as an argument obj.displaymessage(new Message(){ public String greet(){ return "Hello"; );

If you compile and execute the above program, it gives you the following result. Hello This is an example of anonymous inner class as an argument Static Nested Class 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{ Instantiating a static nested class is a bit different from instantiating an inner class. The following program shows how to use a static nested class. public class Outer{ static class Nested_Demo{ public void my_method(){ System.out.println("This is my nested class"); Outer.Nested_Demo nested=new Outer.Nested_Demo(); nested.my_method(); This is my nested class Loading [MathJax]/jax/output/HTML-CSS/jax.js