Java Nested Classes - Exercises, Practice, Solution
Java Nested Classes Exercises [10 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Inner Class:
Write a Java program to create an outer class called Computer with an inner class Processor. The Processor class should have a method "displayDetails()" that prints the details of the processor (e.g., brand and speed). Create an instance of Processor from the Computer class and call the "displayDetails()" method.
2. Static Nested Class:
Write a Java program to create an outer class called University with a static nested class Department. The Department class should have a method "displayInfo()" that prints the department name and the number of faculty members. Instantiate the Department class from the main method and call the "displayInfo()" method.
3. Local Class:
Write a Java program to create a class called Car with a method startEngine(). Inside this method, define a local class Engine that has a method run(). The run() method should print "Engine is running". Instantiate and call the run() method from within the startEngine() method.
4. Anonymous Class Exercise:
Write a Java program to create an interface called Greeting with a method sayHello(). In the main method, create an anonymous class that implements the Greeting interface and override the sayHello() method to print "Hello, World!". Call the sayHello() method.
5. Inner Class Accessing Outer Class Members:
Write a Java program to create an outer class called Library with an instance variable libraryName. Create an inner class Book with a method getLibraryName() that returns the name of the library. Instantiate the Book class and call the getLibraryName() method.
6. Static Nested Class and Static Methods:
Write a Java program to create an outer class called 'MathUtil' with a static nested class Calculator. The Calculator class should have a static method add(int a, int b) that returns the sum of a and b. Call the add() method from the main method.
7. Local Class Accessing Local Variables:
Write a Java program to create a class called House with a method calculateArea(). Inside this method, define a local class Room with a method getArea() that calculates and returns the area of the room (length * width). Instantiate the Room class and call the getArea() method from within calculateArea().
8. Anonymous Class Implementing Abstract Class:
Write a Java program to create an abstract class called Animal with an abstract method makeSound(). In the main method, create an anonymous class that extends Animal and override the makeSound() method to print "Meow" for a cat. Call the makeSound() method.
9. Inner Class with Constructor:
Write a Java program to create an outer class called Person with an inner class Address. The Address class should have a constructor that takes parameters city and state. Create an instance of Address from the Person class and print the address details.
10. Inner Class with Constructor:
Write a Java program to create an outer class called Person with an inner class Address. The Address class should have a constructor that takes parameters city and state. Create an instance of Address from the Person class and print the address details.
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Java Code Editor
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics