Review of Basic Programming Concepts, Using Inner Classes
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.
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.
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:
Syntax:
AnonymousInner an_inner = new AnonymousInner()
{
public void my_method()
{
}
};
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.
Syntax:
obj.my_Method(new My_Class(){ public void Do(){
}
});
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{
}
}
Lab Activities:
Consult the case study of Bank Account as outer and Money as Inner class given in the recommended text book, implement the following tasks.
1: Make an inner class private and access the class through a method, check the access ability of the private inner class outside the outer class.
2: Run the code and check the class object code generated for both the inner and outer classes, verify the syntax of outer_class$innerclass is followed for the class files generated.
3: Convert the code implemented in Lab Task 1 into two separate classes and using the composition for inner class object.
Activity 1:
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();
}
}
Output:
Activity 2:
Access the private members of a class using inner class.
Code:
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{
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:
Use a method-local inner class.
Code:
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();
}
}
Output:
Home Activities:
Implement Vehicle as outer and owner as the inner class, the vehicle class contains vehicle name, engine cc, model as data members. The inner class data members are owners name, CNIC number and phone contact of the owner.
Write down proper setters/ getters and constructors for both the classes.
1: Override the method of a class using anonymous inner class.
2: Pass an anonymous inner class as a method argument.
3: Implement the inner class as static first and then as non static nested class.
Lab Assignment and Viva voce:
Task 1:
Redo the class Person from previous assignments so that the class Date is a private inner class of the class Person. Also, do a suitable test program.
Task 2:
Redo the class Employee and the class HourlyEmployee, so that the class Date is an inner class of the class Employee and an inherited inner class of the class HourlyEmployee. Also, do a suitable test program.