Tuesday 2 July 2019

What is Inner Class in Java? - tccicomputercoaching.com

Java Inner Class or nested class is a class i.e. declared inside the class or interface.

Inner class is like same as regular member of class.
 
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
 
Additionally, it can access all the members of outer class including private data members and methods.


Why we need inner class?

There are so many advantages of inner class as follows:

Advantage of java inner classes

There are basically three advantages of inner classes in java. They are as follows:
 
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
 
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
 
3) Code Optimization: It requires less code to write.

4) Inner class can be private, protected and public any access moditier but outer class never private or protected.

5. Inner class can also be static, final, or abstract.

Real Example:

Car can move, start and stop, wo, we can create class car and describe behaviour in different functions like move (), start () and stop ().

These start and stop is specially related to engine so, we can use inner class engine and these related functions put together into the engine. Such a way we encapsulate behaviour of inner class. We can also describe property horse power of engine.

class car{

void move()
{
}

class Engine
{
int hp;

void start()
{
}

void stop()
{
}
}

There are 3 types:

1. Java Member inner class

A non-static class that is created inside a class but outside a method is called member inner class.

2. Method inner class

Here, inner class is defined inside the method. So, scope of class is only restricted to that method.

3. Anonymous Inner Class

An inner class declared without a class name is known as an anonymous inner class.

TCCI teach various courses like Basic Computer Course , Programming Course , Web Designing Course, Project Training Course, Web Development Course, Typing, SEO , Coaching for All Engineering Branch, Maths Coaching, School Computer Course Coaching

TCCI provide you awesome teaching in all courses with best practical examples, which will remain with your whole life.

So, to get admission at TCCI

Call us @ 98256 18292.

Visit us @ http://tccicomputercoaching.com/engineering-courses/

No comments:

Post a Comment