w3resource

Java program to demonstrate Inner Class Functionality

Java Nested Classes: Exercise-1 with Solution

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.

Sample Solution:

Java Code:

Computer.java

// Outer class called Computer
class Computer {

    // Inner class called Processor
    class Processor {
        // Method to display processor details
        void displayDetails() {
            // Print details of the processor
            System.out.println("Processor Brand: Intel");
            System.out.println("Processor Speed: 3.5 GHz");
        }
    }

    // Method to create and display Processor details
    void showProcessorDetails() {
        // Create an instance of the inner class Processor
        Processor processor = new Processor();
        // Call the displayDetails() method
        processor.displayDetails();
    }

    // Main method to execute the program
    public static void main(String[] args) {
        // Create an instance of the outer class Computer
        Computer computer = new Computer();
        // Call the method to show processor details
        computer.showProcessorDetails();
    }
}

Output:

Processor Brand: Intel
Processor Speed: 3.5 GHz

Explanation:

  • Outer class called Computer: Defined the outer class Computer.
  • Inner class called Processor: Defined the inner class Processor inside the Computer class.
  • Method to display processor details: Created the method displayDetails() in the Processor class to print the processor brand and speed.
  • Method to create and display Processor details: Added a method showProcessorDetails() in the Computer class to create an instance of the Processor class and call the displayDetails() method.
  • Main method to execute the program: Created the main method to instantiate the Computer class and call the showProcessorDetails() method to display the processor details.

Note on Java Nested Classes:

In the above exercise, the static members work by:

In this exercise, the inner class Processor is defined within the outer class Computer. The above exercise demonstrates the use of nested classes in Java, allowing the inner class to access the members of the outer class. By creating an instance of the inner class from within the outer class, we can encapsulate the functionality and maintain a logical grouping of related classes. This helps in organizing code and enhancing readability and maintainability.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Java Nested Classes Exercises Home.
Java Static Members Next: Java Program for Static Nested Class - University and Department.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/nested-classes/java-nested-classes-exercise-1.php