[go: up one dir, main page]

0% found this document useful (0 votes)
33 views16 pages

18 Day Regular Inner Class 1

The document discusses Regular Inner Classes in Java, which are non-static nested classes that can access all members of their outer class, including private ones. It covers how to declare and instantiate inner classes, the rules regarding inheritance, and the ability to implement interfaces and abstract classes within inner classes. Additionally, it highlights the limitations and syntax related to accessing members of both inner and outer classes.

Uploaded by

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

18 Day Regular Inner Class 1

The document discusses Regular Inner Classes in Java, which are non-static nested classes that can access all members of their outer class, including private ones. It covers how to declare and instantiate inner classes, the rules regarding inheritance, and the ability to implement interfaces and abstract classes within inner classes. Additionally, it highlights the limitations and syntax related to accessing members of both inner and outer classes.

Uploaded by

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

Regular Inner class

Inner class is Categorized in 2 category


1)Static Inner class
2) Non Static Inner class
2.1) Regular Inner class

2.2) Method Local Inner class

2.3) Anonymous Inner class

2.1) Member Inner Class / Regular Inner class:


Declaring a Normal class/Regular class [Non static class] inside a class is called as member inner

class.

EX:

class Outer {

class Inner {

----

----

} // inner class end

} //outer class end

Ex:

[Outer.java]

//Regular Inner Class Demo


class Outer{
int x=10; //instance variable
static int y=20; //class variable
class Inner{
void display() {
System.out.println(x);
System.out.println(y);

1 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

} //method end
} // inner class End
public static void main(String args[]){
Outer o=new Outer();
System.out.println(o.x); //10

Outer.Inner i=o.new Inner();


i.display();

/*
Outer.Inner i=new Outer().new Inner();
i.display();
*/
new Outer().new Inner().display();
} //main end
} //outer class end

-- >If we want to access the members of inner class then we have to create object for the

inner class as well as outer class object also, for this, we have to use the following syntax.

Ex:

Outer.Inner ref_Var = new Outer().new Inner();

--> By default, all outer class members are available to inner class, so we can access outer class

Members inside the inner class, but, Inner class members are not available to outer classes, we are

Unable to access inner class members in outer classes.

Note:

Inner class can use each and every thing of the outer class (Including Private members)

Ex:

class Outer {
private int x=10; //instance variable
//instance method
public void makeInner() {
System.out.println(x);
Inner i=new Inner(); // make an inner instance
i.seeOuter();
} //method end
class Inner{

2 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

public void seeOuter(){


System.out.println("Outer x is " + x); //okk
}
} //Inner class End
public static void main(String args[]) {
Outer o=new Outer();
o.makeInner();

/*
Outer.Inner i=o.new Inner();
i.seeOuter();
*/

} // main end
} //outer class end

Regular Inner classes (Non-static nested classes) have access to other members of the outer class/
enclosing class/container class, even if they are declared private.

--> By using outer class reference variable we are able to access only outer class members, we are

unable to access inner class members.

--> By using Inner class reference variable we are Able to access only inner class members we are

unable to access outer class members

3 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class
--> Member inner classes /Regular Inner Classes are not allowing static declarations directly, but, static

Keyword is allowed along with final keyword.

Note:

But From JDK 17 We Can do the static declaration in the Regular Inner classes also.

EX:

[A.java]
class A {
void m1() {
System.out.println("m1-A");
}
class B {
static int i=10; //Error, But From JDK 17 it is also okk
static final int j=20; //Okk
void m2() {
System.out.println("m2-B");
System.out.println(j);
}
void m3() {
System.out.println("m3-B");
}
} //Inner class End
} //Outer class End
[Tester.java]
class Tester{
public static void main(String[] args) {
A a=new A(); //Outer class object creation
a.m1(); //Ok
//a.m2(); // Error /CTE
A.B ab=new A().new B();
ab.m2(); //ok
ab.m3(); //ok

ab.m1(); //--> Error


} //main end
} // Tester class end

4 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

Inheritance over inner class:

In Member inner classes / Regular Inner class, we can extend one inner class to another inner class but
both the inner classes must be provided with in the same outer class.

EX:

class A {
class B {
void m1() {
System.out.println("m1-B");
}
void m2()
{
System.out.println("m2-B");
}
} // Inner class end
class C extends B //Ok bcz B and C are from the same outer class
{
void m3() {
System.out.println("m3-C");
}
void m4(){
System.out.println("m4-C");
}
} //Inner class End
} //Outer class End
class Tester {
public static void main(String[] args) {
A.B ab=new A().new C(); //Upcasting
ab.m1();
ab.m2();
//ab.m3(); //--> Error Method check with reference & call with object
A.C ac=new A().new C();
ac.m1();
ac.m2();
ac.m3();
ac.m4();
}
}

5 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

We can not inherit one inner class to another inner class which are available in two different
outer classes.

EX:

class A {
class B {
---
} //inner class end
} //Outer class end
class C {
class D extends A.B //CTE because B is from diff outer class
{
---
}
}
Status: Invalid.

We can inherit an External class in a regular inner class.


EX:

class A{
void m1(){
System.out.println("I am A.m1");
}
} //External class end
class B{

class C extends A //It is okk


{
void m2(){
System.out.println("I am C.m2");
}

}
}
public class Tester{
public static void main(String[] args){
B.C bc=new B().new C();
bc.m1();
bc.m2();

6 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

}
}
Status: Valid

We can inherit the immediate outer class to its inner class.


EX:

class A

class B extends A

---

Interface inside a class:


Que)

Is it possible to write an interface inside a class?

Ans:

Yes, it is possible to provide an interface inside a class, but, the respective implementation

class must be provided within the same outer class.

EX:

class A {
interface I {
void m1();
void m2();
void m3();
} //Inner interface end
class B implements I {
public void m1() {
System.out.println("m1-B");
}

7 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

public void m2() {


System.out.println("m2-B");
}
public void m3() {
System.out.println("m3-B");
}
} //Inner class end
} //outer class End
class Tester {
public static void main(String[] args)
{
A.I ai=new A().new B(); //Upcasting
ai.m1();
ai.m2();
ai.m3();
}
}

Abstract class Inside class:


Que)
Is it possible to declare an abstract class inside a class

Yes

EX:
class A {
abstract class B {
void m1() {
System.out.println("m1-B");
}
abstract void m2();
abstract void m3();
} //inner class end

8 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

class C extends B {
void m2() {
System.out.println("m2-C");
}
void m3()
{
System.out.println("m3-C");
}
} //Inner class end
} //Outer class End
class Tester {
public static void main(String[] args) {
A.B ab=new A().new C(); // Upcasting
ab.m1();
ab.m2();
ab.m3();
}
}
Class inside Abstract class:

EX:

abstract class A {

class B {

void m1(){

System.out.println("m1-B");

void m2(){

System.out.println("m2-B");

9 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class
void m3(){

System.out.println("m3-B");

} //Inner class end

} //Outer class End

class C extends A{

// class B is coming from abstract class A as a member inner class.

/*

class B

----

----

*/

} //C class End

class Tester{

public static void main(String[] args){

A.B ab = new C().new B();

// C.B ab=new C().new B();

ab.m1();

ab.m2();

ab.m3();

10 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

Abstract class inside an Abstract class:

EX:

abstract class A

abstract class B

void m1()

System.out.println("m1-B");

abstract void m2();

abstract void m3();

} //Inner class end

class C extends B

@Override

void m2()

System.out.println("m2-C");

11 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class
}

@Override

void m3()

System.out.println("m3-C");

} //Inner class end

} //Abstract Outer class end

class D extends A

/*

Now B and C has become the part of D class Because we are inheriting A, and B & C

are the Part of A that is why they have become the part of D

} // D class End

class Tester

public static void main(String[] args)

A.B ab = new D().new C();

ab.m1();

ab.m2();

ab.m3();

12 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

Interface inside an abstract class:

Ex:

abstract class A

interface I

void m1();

void m2();

void m3();

} // Inner Interface end

class B implements I

@Override

public void m1()

System.out.println("m1-B");

@Override

public void m2()

13 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class
System.out.println("m2-B");

@Override

public void m3()

System.out.println("m3-B");

} //B class End

} //A class End

class C extends A

class Tester

public static void main(String[] args)

A.I ai = new C().new B(); //Upcasting

ai.m1();

ai.m2();

ai.m3();

14 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class

Important Facts about Regular inner class


//Inner class can access all things of outer class

class Outer

int x=10; //instance variable

class Inner

int x=40;

void show()

System.out.println(x); //40

System.out.println(Outer.this.x); //10

} // Inner class End

public static void main(String[] args)

Outer o=new Outer();

Outer.Inner i=o.new Inner();

//or

//Outer.Inner i=new Outer().new Inner();

i.show();

15 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)
Regular Inner class
--> Here(In case of Regular inner class) inheritance is also not allowed It means we can not inherit

Regular inner class Directly from the external Outer class.

class Tester extends Outer.Inner //CTE

//Not allowed

16 DD Sir Infomatics, Agra-Whats App No. 9760433226 Online Training Courses


C,DSA,Java,Python,Web Develoment ETC… Visit www.careercompiler.com
Prepared By DD Singh,Coding Career Expert(18+ Years’ Experience)

You might also like