0% found this document useful (0 votes)
46 views31 pages

FALLSEM2021-22 STS3201 SS VL2021220100099 Reference Material I 03-09-2021 Inner Classes

The private static nested class Bone cannot be accessed from outside the outer class Dog. Therefore, the statement Dog.Bone.size = 5; will result in a compile-time error. The correct answer is B. D. Bone of size null static void stopBark(int size) { Bone.size = size; System.out.println("Bone of size " + Bone.size); } } class Main { public static void main(String[] args) { Dog.Bone.size = 5; Dog.stopBark(5); } } Answer: B

Uploaded by

Shashwat Ashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views31 pages

FALLSEM2021-22 STS3201 SS VL2021220100099 Reference Material I 03-09-2021 Inner Classes

The private static nested class Bone cannot be accessed from outside the outer class Dog. Therefore, the statement Dog.Bone.size = 5; will result in a compile-time error. The correct answer is B. D. Bone of size null static void stopBark(int size) { Bone.size = size; System.out.println("Bone of size " + Bone.size); } } class Main { public static void main(String[] args) { Dog.Bone.size = 5; Dog.stopBark(5); } } Answer: B

Uploaded by

Shashwat Ashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Inner Classes

Introduction

Inner class means one class which is a member of another class. There are basically
four types of inner classes in java.

● Nested Inner class


● Method Local inner classes
● Anonymous inner classes
● Static nested classes
NESTED INNER CLASSES

Nested Inner class can access any private instance variable of outer class. Like any
other instance variable, we can have access modifier private, protected, public and
default modifier.

Like class, interface can also be nested and can have access specifiers.
NESTED INNER CLASSES

class Outer {
class Inner {
public void show() {
System.out.println("In a nested class method");
}
}
}
class Main {
public static void main(String[] args) {
Outer.Inner in = new Outer().new Inner();
in.show();
}
}
STATIC AND INNER CLASS

We can’t have static method in a nested inner class because an inner class is
implicitly associated with an object of its outer class so it cannot define any
static method for itself
class Outer {
void outerMethod() {
System.out.println("inside outerMethod");
}
class Inner {
public static void main(String[] args){
System.out.println("inside inner class Method");
}
}
}

The above code will lead to an error


METHOD LOCAL INNER CLASSES

Inner class can be declared within a method of an outer class. In the following
example, Inner is an inner class in outerMethod().
METHOD LOCAL INNER CLASSES

class Outer {
void outerMethod() {
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
System.out.println("inside innerMethod");
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer x = new Outer();
x.outerMethod();
}
}
METHOD LOCAL INNER CLASSES

class Outer {
void outerMethod() {
int x = 98;
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
System.out.println("x= "+x);
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer x=new Outer();
x.outerMethod();
}
}
METHOD LOCAL INNER CLASSES

class Outer {
void outerMethod() {
int x = 98;
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
int x = 100;
System.out.println("x= "+x);
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer x=new Outer();
x.outerMethod();
}
}
STATIC NESTED CLASSES

class Outer {
private static void outerMethod() {
System.out.println("inside outerMethod");
Static nested classes }
static class Inner {
are not technically an static void method() {
inner class. They are System.out.println("inside inner class Method");
like a static member outerMethod();
}
of outer class. }
}
class Main {
public static void main(String args[]) {
Outer.Inner.method();
}
}
ANONYMOUS INNER CLASSES

Anonymous inner classes are declared without any name at all. They are created in
two ways.
● As subclass of specified type
● As implementer of the specified interface

We are going to create have two class Demo and Main. Here Demo act as super class
and anonymous class acts as a subclass, both classes have a method show(). In
anonymous class show() method is overridden.
METHOD LOCAL INNER CLASSES

class Demo {
void show() {
System.out.println("I am in show method of super class");
}
}
class Main {
static Demo d = new Demo() {
void show() {
super.show();
System.out.println("I am in Main class");
}
};
public static void main(String[] args){
d.show();
}
}
AS IMPLEMENTER OF THE SPECIFIED INTERFACE

class Main {
static Hello h = new Hello() {
public void show() {
System.out.println("I am in anonymous class");
}
};
public static void main(String[] args) {
h.show();
}
}
interface Hello {
void show();
}
AS IMPLEMENTER OF THE SPECIFIED INTERFACE

We have created an object of anonymous inner class but this anonymous inner
class is an implementer of the interface Hello.

Any anonymous inner class can implement only one interface at one time. It can
either extend a class or implement interface at a time.
QUESTION

What is NOT a type of inner classes?

A. Nested Inner class

B. Method Local inner class

C. Anonymous inner class

D. Dynamic inner class

Answer: D
QUESTION

Non-static nested classes have access to _____________ from enclosing class.

A. Private members

B. Protected members

C. Public members

D. All members

Answer: D
QUESTION

Use of nested class ____________ encapsulation.

A. Increases

B. Decreases
C. Doesn’t affect
D. Slightly decreases

Answer: A
QUESTION

A nested class can have its own static members.

A. True
B. False

Answer: B
QUESTION

How to create object of the inner class?

A. OuterClass.InnerClass innerObject = outerObject.new InnerClass();


B. OuterClass.InnerClass innerObject = new InnerClass();
C. InnerClass innerObject = new outerObject.InnerClass();
D. OuterClass.InnerClass = outerObject.new InnerClass();

Answer: A
QUESTION

If a declaration of a member in inner class has the same name as that in the
outer class, then ________________ enclosing scope

A. Outer declaration shadows inner declaration in


B. Inner declaration shadows outer declaration in
C. Declaration gives compile time error
D. Declaration gives runtime error

Answer: B
QUESTION

class Dog {
String name;
class Bone {
A. Bone of size 5
int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone b = new Bone();
b.size = size;
System.out.println("Bone of size " + b.size);
}
}
class Main {
public static void main(String[] args) {
Dog.stopBark(5);
}
} Answer: B
QUESTION

class Dog {
String name;
static class Bone {
A. Bone of size 5
static int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone.size = size;
System.out.println("Bone of size " + Bone.size);
}
}
class Main {
public static void main(String[] args) {
Dog.stopBark(5);
}
}
Answer: A
QUESTION

class Dog {
String name;
static class Bone {
A. Bone of size 5
int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone.size = size;
System.out.println("Bone of size " + Bone.size);
}
}
class Main {
public static void main(String[] args) {
Dog.stopBark(5);
}
}
Answer: A
QUESTION

class Dog {
String name;
static class Bone {
A. Bone of size 5
private static int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone.size = size;
System.out.println("Bone of size " + Bone.size);
}
}
class Main {
public static void main(String[] args) {
Dog.Bone.size = 5;
Dog.stopBark(5);
}
} Answer: B
QUESTION

class Dog {
String name;
private static class Bone {
A. Bone of size 5
static int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone.size = size;
System.out.println("Bone of size " + Bone.size);
}
}
class Main {
public static void main(String[] args) {
Dog.Bone.size = 5;
Dog.stopBark(5);
}
} Answer: B
QUESTION

class Dog {
String name;
class Bone {
A. Bone of size 5
static int size; B. Error
} C. Bone of size 0
static void stopBark(int size) { D. Bone of size null
Bone.size = size;
System.out.println("Bone of size " + Bone.size);
}
}
class Main {
public static void main(String[] args) {
Dog.Bone.size = 5;
Dog.stopBark(5);
}
} Answer: B
QUESTION

class Outer {
static int x = 98;
void outerMethod() {
A. x= 100
static class Inner { B. Error
void innerMethod() { C. x= 98
x = 100; D. x= 0
System.out.println("x= " + x);
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer x=new Outer(); Answer: B
x.outerMethod();
}
}
QUESTION

class Outer {
class Inner {
void innerMethod() {
A. x= 100
int x = 100; B. NullPointerException
System.out.println("x= " + x); C. Error in creating object
} D. x= 0
}
}
class Main {
public static void main(String[] args) {
Outer x = new Outer();
Outer.Inner i = x.new Inner();
x = null;
i.innerMethod();
}
} Answer: B
QUESTION

class Outer {
class Inner {
void innerMethod() {
A. x= 100
int x = 100; B. NullPointerException
System.out.println("x= " + x); C. Error: Accessing non static
} from static context
}
} D. x= 0
class Main {
public static void main(String[] args) {
Outer x = new Outer();
x.Inner.innerMethod();
}
}

Answer: C

You might also like