1. Write a program to create an outer class Person that has a private field name. Create an inner class Address inside the Person class that contains a private field address. Write a method displayInfo() in the inner class to print both name and address from the outer class?
class Person { private String name; public Person(String name) { this.name = name; } class Address { private String address; public Address(String address) { this.address = address; } public void displayInfo() { System.out.println(“Name: ” + name); System.out.println(“Address: ” + address); } } public static void main(String[] args) { Person person = new Person(“John”); Person.Address address = person.new Address(“1234 Elm Street”); address.displayInfo(); } } |
Output :
Name: John Address: 1234 Elm Street |
2. Write a program to create a static inner class Employee within an outer class Department. The Employee class should have id, name, and salary attributes. Write a method to display the employee’s details?
class Department { static class Employee { private int id; private String name; private double salary; public Employee(int id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public void displayEmployeeDetails() { System.out.println(“ID: ” + id); System.out.println(“Name: ” + name); System.out.println(“Salary: ” + salary); } } public static void main(String[] args) { Department.Employee emp = new Department.Employee(101, “Ram”, 50000); emp.displayEmployeeDetails(); } } |
Output :
ID: 101 Name: Ram Salary: 50000.0 |
3. Write a Java program where a non-static inner class Calculator can access the private field number in the outer class MathOperations. The inner class should implement methods for addition and subtraction of a given number and display the results?
class MathOperations { private int number; public MathOperations(int number) { this.number = number; } class Calculator { public void add(int num) { System.out.println(“Addition: ” + (number + num)); } public void subtract(int num) { System.out.println(“Subtraction: ” + (number – num)); } } public static void main(String[] args) { MathOperations mathOp = new MathOperations(10); MathOperations.Calculator calc = mathOp.new Calculator(); calc.add(5); calc.subtract(3); } } |
Output :
Addition: 15 Subtraction: 7 |
4. Write a program to create an interface Shape with methods area() and perimeter() implement the Shape interface using an anonymous inner class inside the main method to calculate the area and perimeter of a rectangle. The length and width of the rectangle should be passed as parameters?
interface Shape { double area(); double perimeter(); } public class ShapeTest { public static void main(String[] args) { Shape rectangle = new Shape() { private double length = 5; private double width = 3; @Override public double area() { return length * width; } @Override public double perimeter() { return 2 * (length + width); } }; System.out.println(“Area: ” + rectangle.area()); System.out.println(“Perimeter: ” + rectangle.perimeter()); } } |
Output :
Area: 15.0 Perimeter: 16.0 |
5. Write a Java program that uses a local inner class inside a method. The local inner class should take two integers as input and perform the operation of multiplying them. The result should be printed inside the method?
public class Calculator { public void multiplyNumbers(int a, int b) { class Multiplier { public void multiply() { System.out.println(“Multiplication Result: ” + (a * b)); } } Multiplier multiplier = new Multiplier(); multiplier.multiply(); } public static void main(String[] args) { Calculator calc = new Calculator(); calc.multiplyNumbers(4, 5); } } |
Output :
Multiplication Result: 20 |
6. Write a Java program where an inner class Printer accesses the outer class Book’s title using this keyword?
class Book { private String title; public Book(String title) { this.title = title; } class Printer { public void printTitle() { System.out.println(“Book title: ” + Book.this.title); } } public static void main(String[] args) { Book book = new Book(“Java Programming”); Book.Printer printer = book.new Printer(); printer.printTitle(); } } |
Output :
Book title: Java Programming |
7. Write a program to create a class Bank that contains a non-static inner class Account. The Account class should have methods to deposit, withdraw, and check balance. The outer class should allow for managing accounts, and it should print account details?
class Bank { private String bankName; public Bank(String bankName) { this.bankName = bankName; } class Account { private double balance; public Account(double initialBalance) { this.balance = initialBalance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { System.out.println(“Insufficient balance”); } } public double getBalance() { return balance; } public void accountDetails() { System.out.println(“Bank: ” + bankName); System.out.println(“Balance: ” + balance); } } public static void main(String[] args) { Bank bank = new Bank(“MyBank”); Bank.Account account = bank.new Account(1000); account.deposit(500); account.withdraw(300); account.accountDetails(); } } |
Output :
Bank: MyBank Balance: 1200.0 |