Sunday, 8 May 2022

Question : Explain the concept of inner classes with suitable examples in java.

 

Inner class(Non Static Nested Class) - a class within another class

 

Java inner class or nested class is a class that is declared inside the class or interface.

एक क्‍लास या एक इंटरफेस के अन्‍दर ही और क्‍लासेज बनाना जावा इनर क्‍लासेज कहलाता है।

 

We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.

इनर क्‍लासेज को उपयोग इसलिए किया जाता है जिससे सभी क्‍लासेज एक ही जगह पर मिल जाये ताकि उनको समझना/उपयोग करना व मेन्‍टेन करना आसान रहे।

 

Additionally, it can access all the members of the outer class, including private data members and methods.

इनर क्‍लासेज आउट क्‍लासेज के प्राइवेट डाटा मेम्‍बर्स व मैथड्स को भी एक्‍सेस कर सकती है।

 

There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.

नेस्‍टेड क्‍लासेज दो प्रकार की होती है। नॉन स्‍टेटिक व स्‍टेटिक। नॉन स्‍टेटिक को ही इनर क्‍लास के नाम से जाना जाता है।

  • Non-static nested class (inner class)

1.       Member inner class

2.       Anonymous inner class

3.       Local inner class

  • Static nested class

 

 

Example(उदाहरण):-

 

class OuterClass

{

  int x = 10;

  class InnerClass

{

    int y = 5;

  }

}

 

public class Main {

  public static void main(String[] args) {

    OuterClass myOuter = new OuterClass();

    OuterClass.InnerClass myInner = myOuter.new InnerClass();

    System.out.println(myInner.y + myOuter.x);

  }

}

 

// Outputs 15 (5 + 10)

1 comment:

  1. The accuracy of deep learning methods was far larger than typical machine learning methods when using binary classification to filter spam messages . We are reader-supported so we could obtain a fee whenever you purchase via hyperlinks on our web site. You do not pay additional for something you buy on our web site — our fee comes directly from the product proprietor. SaferVPN is one other stickler for hardcore safety, no surprise given its country of origin is Israel. It has a inflexible no-logs coverage and a faultless kill switch to maintain you secure, and its encryption is widespread, supporting PPTP, L2TP/IPSec, IKEV2, and OpenVPN connections. 온라인카지노 PrivateVPN relies in Sweden and lives a lot as} its name thanks in large part|largely} to is 2,048-bit encryption, strict no-logs coverage, computerized kill switch, and built-in leak protection.

    ReplyDelete