Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

-----------------------------------------------------------------------------------

----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NESTED
CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

>> When we declare a named class inside another class, is known as a nested class.

Ques 1 :- what is a nested class exaplin all types of nested class with the help of
a example.
Ques 2 : What is the difference b/t normal class and ananomous class, explain with
the help of example.
Ques 3 : What is a ananomous class demostrate the concept with the help of a
programme.

==>> TYPES OF NESTED CLASS :

Static NESTED Class:


Non Static NESTED Class :

>> STATIC NESTED CLASS :


1. We can't declare outer class as a static.
2. The static class can access private
protected and stactic variable of outer class.
3. Static class is collection of static method
as well as non-static methods.

>> INNER NESTED CLASS : It is the part of Non Static Nested class.

class outer
{
class inner
{
}
}
1. It will generate 2 byte code file i.e.
1) outer.class
2)
outer$inner.class

2. We can declare any static method or variable inside the inner class.

3. class outer{
void display()
{
SOP("Outer class method");
}
class inner{
void display()
{
SOP("Inner class method");
}
}
PSVM(String []args)
{
outer o=new outer();
o.display();
outer.inner i=o.new inner();
i.display();
}
}

4. Inner class can directly access any type of a variable of a outer class.

>> METHOD NESTED CLASS :-


1. When we declare a class inside a
method that class is known as method local nested class.
2. The scope of the local class is
limited to that methhod only in which it is declared.
3. It is nessesary to create object of a
local nested class only inside a method in which it is declared.

Syntax :
class outer{
//Outer class
Void display(){
//Method
class inner{
//Method local nested class
}
}
}

-----------------------------------------------------------------------------------
----------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ANONYMOUS
CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------------------------

>> A class having no name is known as Anonymus class.


>> It is used to impliment interface and abstract class.
>> We can't create contructor of anonymus class. Because it is having no name.

--> Syntax :-
parent_class object=new parent_class()
{__________________
__________________
Lines of code
__________________
__________________
}; //Don't forget to put a
semicoloumn.

Ques :-
WAP for creating a 5 ananomous classes

Ques :- WAP to create a class named as IPL_TEAM with attributes Team_name,


wicket_keeper, allrounder, captain.
Provide a constructor to initialize all the attributes.
Player is a nested private class with attributes player_name, price,
create a suitable contructor to initialize the attributes in a main class, Create
atleast 3 teams , each having a captain , a wicket keeper and atleast one all
rounder and store the team in a arraylist.
Impliment a ,method which display name of a team and details of all
players of that team whoes captain is having heightest price.

You might also like