



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lab manual that reviews the concepts related to Inner Classes in Java. It explains how inner classes can be used as helping classes and how to access private members of a class using inner classes. The document also covers the use of static nested classes. The lab manual provides instructions on how to create inner classes and how to access private members of a class using inner classes. It also provides syntax for creating static nested classes. The lab manual recommends reading Chapter 13 of the book Absolute Java by Savitch and Mock as a pre-lab activity.
Typology: Summaries
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Objective of this lab is to review concepts related to Inner Classes, we will describe one of the most useful applications of inner classes, namely, inner classes used as helping classes. An inner class is simply a class defined within another class. Because inner classes are local to the class that contains them, they can help make a class self-contained by allowing you to make helping classes inner classes
Student will learn to make inner class private and access the class through a method. How to access the private members of a class using inner class. How to use a static nested class.
As pre-lab activity, read Chapter 13 from the book, (Absolute Java, Savitch, W. & Mock, K., 5th Edition (2012), Pearson.), and also as given by your theory instructor. LAB # 08
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. 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.
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 (from which you want to access the private members) call the getValue() method of the inner 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: Syntax: class MyOuter { static class Nested_Demo{ } }
}
Activity 2: \Access the private members of a class using inner class.
class Outer_Demo { //private variable o fthe 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_class { 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()); } }
Activity 3: Access the static inner class.
class TestOuter { static int data=30; static class Inner { void msg() {System.out.println("data is "+data);} This is an inner class. The value of num in the class Test is: 175
} public static void main(String args[]) { TestOuter1.Inner obj=new TestOuter1.Inner(); obj.msg(); } }