Working with Nested classes in java ?
--------------------------------------
In java it is possible to define a class (inner class) inside another class (outer
class) called nested class.
In the same way it is also possible to define an interface OR enum OR Annotation or
record inside another interface OR enum OR Annotation or record is called inner
class or nested class.
Inner classes in Java create a strong encapsulation and HAS-A relationship
between the inner class and its outer class.
An inner class, .class file will be represented by $ symbol.
Advantages of inner class :
--------------------------------
1) It is a way of logically grouping classes that are only used in one place.
2) It is used to achieve strong encapsulation.
3) It enhance the readability and maintainability of the code.
Types of Inner classes in java :
--------------------------------
There are 4 types of inner classes in java :
1) Non Static Nested class OR Regular class
2) Static Nested class
3) Method / Method local class
4) Anonymous inner class
Non static Nested class :
--------------------------
If a non static class define inside a class and outside of the method then it is
called Non static nested class.
Outer class object is compulsory for non static nested class.
From non static nested class, we can directly access the private data of outer
class. (Encapsulation)
A non static nested class we can declare with private, default, protected, public,
abstract and final.
It is also known as Member Regular class.
----------------------------------------------------------------------
class Outer
{
private int a = 15;
class Inner
{
public void displayValue()
{
[Link]("Value of a is " + a);
}
}
}
public class Test1
{
public static void main(String... args)
{
Outer out = new Outer();
[Link] in = [Link] Inner();
[Link]();
}
}
Note : An inner class can directly access the private data of Outer class.
----------------------------------------------------------------------
class MyOuter
{
private int x = 7;
public void makeInner()
{
MyInner in = new MyInner();
[Link]("Inner y is "+in.y);
[Link]();
}
class MyInner
{
private int y = 15;
public void seeOuter()
{
[Link]("Outer x is "+x);
}
}
}
public class Test2
{
public static void main(String args[])
{
MyOuter m = new MyOuter();
[Link]();
}
}
-----------------------------------------------------------------------
class MyOuter
{
private int x = 15;
class MyInner
{
public void seeOuter()
{
[Link]("Outer x is "+x);
}
}
}
public class Test3
{
public static void main(String args[])
{
//Creating inner class object in a single line
[Link] m = new MyOuter().new MyInner();
[Link]();
}
}
----------------------------------------------------------------------
class MyOuter
{
static int x = 7;
class MyInner
{
public static void seeOuter() //[Link]();
{
[Link]("Outer x is "+x);
}
}
}
public class Test4
{
public static void main(String args[])
{
[Link]();
}
}
----------------------------------------------------------------------
class Car
{
private String make;
private String model;
private Engine engine;
public Car(String make, String model, int horsePower)
{
[Link] = make;
[Link] = model;
[Link] = new Engine(horsePower);
}
//Inner class
private class Engine
{
private int horsePower;
public Engine(int horsePower)
{
[Link] = horsePower;
}
public void start()
{
[Link]("Engine started! Horsepower: " + horsePower);
}
public void stop()
{
[Link]("Engine stopped.");
}
}
public void startCar()
{
[Link]("Starting " + make + " " + model);
[Link]();
}
public void stopCar()
{
[Link]("Stopping " + make + " " + model);
[Link]();
}
}
public class Test5
{
public static void main(String[] args) {
Car myCar = new Car("Swift", "Desire", 1200);
[Link]();
[Link]();
}
}
----------------------------------------------------------------------
class Laptop
{
private String brand;
private String model;
private Motherboard motherboard;
public Laptop(String brand, String model, String motherboardModel, String
chipset)
{
[Link] = brand;
[Link] = model;
[Link] = new Motherboard(motherboardModel, chipset);
}
public void switchOn()
{
[Link]("Turning on " + brand + " " + model);
[Link]();
}
//Motherboard inner class
public class Motherboard
{
private String model;
private String chipset;
public Motherboard(String model, String chipset)
{
[Link] = model;
[Link] = chipset;
}
public void boot()
{
[Link]("Booting " + brand + " " + model + " with " +
chipset + " chipset");
}
}
}
public class Test6
{
public static void main(String[] args)
{
Laptop laptop = new Laptop("HP", "ENVY", "IRIS", "Intel");
[Link]();
}
}
-----------------------------------------------------------------------
25-06-2024
-----------
class Person
{
private String name;
private int age;
private Heart heart;
public Person(String name, int age)
{
[Link] = name;
[Link] = age;
[Link] = new Heart();
}
public void describe()
{
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Heart beats per minute: " + [Link]());
}
// Inner class representing the Heart
private class Heart
{
private int beatsPerMinute;
public Heart()
{
[Link] = 72;
}
public int getBeatsPerMinute()
{
return [Link];
}
public void setBeatsPerMinute(int beatsPerMinute)
{
[Link] = beatsPerMinute;
}
}
}
public class Test7
{
public static void main(String[] args)
{
Person person = new Person("Virat", 30);
[Link]();
}
}
-----------------------------------------------------------------------
class University
{
private String name;
public University(String name)
{
[Link] = name;
}
public void displayUniversityName()
{
[Link]("University Name: " + name);
}
public class Department
{
private String name;
private String headOfDepartment;
public Department(String name, String headOfDepartment)
{
[Link] = name;
[Link] = headOfDepartment;
}
// Method to display department details
public void displayDepartmentDetails()
{
displayUniversityName();
[Link]("Department Name: " + name);
[Link]("Head of Department: " + headOfDepartment);
}
}
}
public class Test8
{
public static void main(String[] args)
{
University university = new University("JNTU");
[Link] cs = [Link] Department("Computer Science",
"Dr. John");
[Link] ee = [Link] Department("Electrical
Engineering", "Dr. Scott");
[Link]();
[Link]();
}
}
-----------------------------------------------------------------------
class OuterClass
{
private int x = 200;
class InnerClass
{
public void display() //Inner class display method
{
[Link]("Inner class display method");
}
public void getValue()
{
display();
[Link]("Can access outer private var :"+x);
}
}
public void display() //Outer class display method
{
[Link]("Outer class display");
}
}
public class Test9
{
public static void main(String [] args)
{
[Link] inobj = new OuterClass().new InnerClass();
[Link]();
new OuterClass().display();
}
}
Note :- inner class display() will be invoked from inner class object, if we want
to access display() method of Outer class then Outer class
object is required.
-----------------------------------------------------------------------
class OuterClass
{
int x;
public class InnerClass //private, def, prot, public, abstract
{ //final
int x;
}
}
public class Test10
{
}
----------------------------------------------------------------------
Method OR Method Local inner class :
-------------------------------------
If a class is defined inside a method with class name then it is called
Method local inner class.
On method local inner class, we can apply only abstract and final modifier.
A method local inner class must be accessible within the same method only that
means we can't access method local inner class outside of the method.
-----------------------------------------------------------------------
//program on method local inner class
class MyOuter3
{
private String x = "Outer class private data";
public void doSttuff()
{
String z = "local variable";
class MyInner //only final and abstract is possible
{
public void seeOuter()
{
[Link]("Outer x is "+x);
[Link]("Local variable z is : "+z);
}
}
MyInner mi = new MyInner();
[Link]();
}
public class Test11
{
public static void main(String args[])
{
MyOuter3 m = new MyOuter3();
[Link]();
}
}
-----------------------------------------------------------------------
//local inner class we can't access outside of the method
class MyOuter3
{
private String z = "Outer class Data";
public void doSttuff()
{
String x = "local variable";
class MyInner
{
String z = "CLASS instance variable";
public void seeOuter()
{
[Link]("Outer x is "+[Link].z);
[Link]("Class variable z is : "+this.z);
[Link]("Local variable z is : "+x);
}
}
MyInner mi = new MyInner();
[Link]();
}
}
public class Test12
{
public static void main(String args[])
{
MyOuter3 m = new MyOuter3();
[Link]();
}
}
-----------------------------------------------------------------------
//Variable Shadow
public class Test
{
private int x = 100;
public void methodLocal()
{
class Hello
{
private int x = 200; //Variable Shadow
public void access()
{
[Link]("x value is :"+this.x);
[Link]("x value is :"+[Link].x);
}
}
Hello h = new Hello();
[Link]();
}
public static void main(String[] args)
{
new Test().methodLocal();
}
}
-----------------------------------------------------------------------
static Nested class :
---------------------
If we declare a static class inside an outer class then it is called
static nested class.
For static nested inner class, Outer class object is not required.
If the static nested inner class, contains only static member then to access those
members, static nested inner class object is also not required.
If the static nested inner class contains non static member then object is required
for static nested inner class.
From static nested inner class, we can't access non static member of Outer class.
Example :
----------
public class Outer
{
static class Inner //static nested inner class
{
}
}
[Link] in = new [Link]();
----------------------------------------------------------------------
//static nested inner class
class BigOuter
{
static class Nest //static nested inner class
{
void go() //Instance method of static inner class
{
[Link]("Hello welcome to static nested class");
}
}
}
class Test13
{
public static void main(String args[])
{
[Link] n = new [Link]();
[Link]();
}
}
----------------------------------------------------------------------
class Outer
{
static int x = 15;
static class Inner
{
void msg()
{
[Link]("x value is "+x);
}
}
}
class Test14
{
public static void main(String args[])
{
[Link] obj=new [Link]();
[Link]();
}
}
-----------------------------------------------------------------------
class Outer
{
static int x = 25;
static class Inner
{
static void msg()
{
[Link]("x value is "+x);
}
}
}
class Test15
{
public static void main(String args[])
{
[Link]();
}
}
-----------------------------------------------------------------------
class Outer
{
int x=15; //error (not possible because try to access instance variable)
static class Inner
{
void msg()
{
[Link]("x value is "+x);
}
}
}
class Test16
{
public static void main(String args[])
{
[Link] obj=new [Link]();
[Link]();
}
}
----------------------------------------------------------------------
Anonymous inner class :
------------------------
If we define class inside a method body without any name then it is called
anonymous inner class.
The main purpose of anonymous inner class to extend a class or implement an
ineterface that means creating sub type of a class or interface.
Anonymous inner class object will be created at the time of declaration the class
body.
Anonymous inner class is used to create singleton object because for 1 anonymous
inner class only 1 object will be created.
A normal concrete class can extend only one class as well as implement many number
of interfacs but an anoymous inner class can either extend only one class OR can
implement only one interaface only.
Inside an anonymous inner class we can write static , non static variable, static
block and non-static block. Here we can't write abstract method and constructor.
-----------------------------------------------------------------------
//Anonymous inner class
class Tech
{
public void tech()
{
[Link]("Tech");
}
}
public class Test17
{
public static void main(String... args)
{
Tech a = new Tech() //Anonymous inner class
{
@Override
public void tech()
{
[Link]("anonymous tech");
}
};
[Link]();
}
}
----------------------------------------------------------------------
@FunctionalInterface
interface Vehicle
{
void move(); //SAM(Single Abstract Method)
}
class Test18
{
public static void main(String[] args)
{
Vehicle car = new Vehicle()
{
@Override
public void move()
{
[Link]("Moving with Car...");
}
};
[Link]();
Vehicle bike = new Vehicle()
{
@Override
public void move()
{
[Link]("Moving with Bike...");
}
};
[Link]();
}
}
-----------------------------------------------------------------------