Join Regular Classroom : Visit ClassroomTech

Programming in JAVA – codewindow.in

Related Topics

JAVA Programming

Explain the use of inner classes in Java and give an example of using inner classes?

In Java, an inner class is a class that is defined inside another class. Inner classes are used to encapsulate related functionality and to create a more readable and maintainable code.

Inner classes can be of four types:

  1. Member Inner Class: It is a non-static inner class that is defined inside a class. It can access all the members of the outer class, including private members.

  2. Local Inner Class: It is an inner class that is defined inside a method or a block. It can access the final or effectively final variables of the enclosing method or block.

  3. Anonymous Inner Class: It is an inner class that is declared and instantiated at the same time, without giving it a name. It is often used for event handling or to implement interfaces.

  4. Static Nested Class: It is a static inner class that is defined inside another class. It can access only the static members of the outer class.

Here is an example of using a member inner class in Java:

public class OuterClass {
    private int outerVar;

    public class InnerClass {
        public void display() {
            System.out.println("Value of outerVar: " + outerVar);
        }
    }

    public void callInner() {
        InnerClass innerObj = new InnerClass();
        innerObj.display();
    }

    public static void main(String[] args) {
        OuterClass outerObj = new OuterClass();
        outerObj.callInner();
    }
}

In this example, InnerClass is a member inner class that is defined inside OuterClass. The display method of InnerClass is able to access the private member outerVar of OuterClass. In the callInner method, an instance of InnerClass is created and its display method is called.

When the main method is executed, an instance of OuterClass is created and its callInner method is called. The callInner method creates an instance of InnerClass and calls its display method, which displays the value of outerVar.

What is the difference between an object and an instance in Java?

In Java, the terms “object” and “instance” are often used interchangeably, but they do have slightly different meanings.

An object is a software entity that contains data and behavior. It is an instance of a class and can be created using the new keyword.

On the other hand, an instance is a specific occurrence of an object. When you create an object, you are creating an instance of that object. For example, if you create an object of the Person class, you are creating an instance of the Person class.

In other words, an object is a blueprint that defines the behavior and data of a software entity, while an instance is a specific realization of that blueprint.

Here’s an example to illustrate the difference between object and instance in Java:

public class Person {
   private String name;

   public Person(String name) {
      this.name = name;
   }

   public void sayHello() {
      System.out.println("Hello, my name is " + name);
   }
}

public class Main {
   public static void main(String[] args) {
      Person person1 = new Person("Alice");
      Person person2 = new Person("Bob");

      person1.sayHello(); // Output: Hello, my name is Alice
      person2.sayHello(); // Output: Hello, my name is Bob
   }
}

In this example, Person is a class that defines the behavior and data of a person. When new Person("Alice") is executed, a new object of the Person class is created. This object is an instance of the Person class, and its name field is set to "Alice". Similarly, when new Person("Bob") is executed, another instance of the Person class is created with its name field set to "Bob". Both person1 and person2 are objects of the Person class, but they are different instances of that class with different data.

Questions on Chapter 7

Questions on Chapter 8

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories