Review of Inner Classes in Java, Summaries of Object Oriented Programming

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

2020/2021

Available from 01/18/2022

technical-ibiii
technical-ibiii 🇵🇰

2 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Statement Purpose:
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
Activity Outcomes:
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.
Instructor Note:
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
pf3
pf4
pf5

Partial preview of the text

Download Review of Inner Classes in Java and more Summaries Object Oriented Programming in PDF only on Docsity!

Statement Purpose:

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

Activity Outcomes:

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.

Instructor Note:

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

1) Stage J (Journey)

Introduction

Inner Classes:

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.

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 (from which you want to access the private members) call the getValue() method of the inner class.

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: Syntax: class MyOuter { static class Nested_Demo{ } }

}

Output :

Activity 2: \Access the private members of a class using inner class.

Code:

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()); } }

Output:

Activity 3: Access the static inner class.

Code:

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(); } }