This is slide presentation that describes inner classes in java and illustrates the concept using demo examples with code shared on both GitHub and dev community.
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Â
Inner Classes in Java
1. INNER CLASSES IN JAVA
https://dev.to/dallington256/inner-classes-or-nested-classes-
in-java-4m8d
2. Objectives
â–ŞWhat are inner classes
â–ŞWhy do we need inner classes
â–ŞAdvantages of using inner classes
â–ŞTypes of inner classes
â–ŞCode examples of each type of inner classes
3. Definition
An inner class is a class that is declared inside a class or
interface.
Inner classes are used to logically group classes or interfaces in
one place in order to improve readability and maintainability.
Creating an inner class is quite simple. You just need to write a
class within a class. Unlike a class, an inner class can be private
and once you declare an inner class private, it cannot be accessed
from an object outside the class
4. Syntax of an inner class
class x1 {
// code
class x2{
// code
}
}
5. Why do we need an inner class
There might be a program where a class must not be accessed
by the other class so in that case, you include it in another
class.
Inner classes are a security mechanism in Java. We know a
class cannot be associated with the access modifier private,
but if we have the class as a member of other class, then the
inner class can be made private. And this is also used to
access the private members of a class
6. Advantages of using inner classes
â–ŞBecause inner classes logically group classes and interfaces
in one place only, they improve readability and
maintainability.
â–ŞInner classes are a representation of a particular type of
relationship in that inner class can access all the members
(data members and methods) of an outer class including one
declared private.
▪Minimizes coding efforts since there’s less code to write
thus facilitating code optimization.
7. Key facts on the relationship between
inner and outer classes
▪Inner and outer classes have access to each other’s private
members.
Within the definition of a method of an inner classes;
âť– It is legal to reference a private instance variable of the
outer class
âť– It is legal to invoke a private method of the outer class
8. Key facts on the relationship between
inner and outer classes
Within the definition of a method of the outer classes;
âť– It is legal to reference a private instance variable of the
inner class on an object of the inner class.
âť– It is legal to invoke a (non-static) method of the inner class
as long as an object of the inner class is used as a calling
object.
âť– Within the definition of the inner or outer class, the
modifiers public and private are equivalent.
9. Types of inner classes
There are 4 types of inner classes i.e..
1. Member class (Nested inner class)
2. Static member inner class (Static nested class)
3. Anonymous inner class
4. Local inner class.
10. 1. Member inner class
A member inner class is a class created within a class and
outside a method. A member inner class is non-static.
A member inner class refers directly to its instance variables or
methods defined in its enclosing class. Also because an inner
class is associated with an instance, it cannot define any static
members itself.
11. Syntax for member inner class
class MainClass{
// code
class InnerClass{
// code
}
12. Instantiation of a member inner class
Syntax for creating an object of a non-static member
inner class
MainClass m = new MainClass();
MainClass.InnerClass innerObj = m.new InnerClass()
13. Example
Write simple example to demonstrate member inner class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
14. 2. Static member inner class
A static member inner class is an inner class that is declared static.
Syntax for a static inner class
class MainClass {
// code
static class StaticInnerClass {
// code
}
}
15. Continue…
In the above code snippet, StaticInnerClass is a static inner class.
This class can’t refer directly to their instance variables or methods
defined in its enclosing class since they use them only by an object
reference.
They are accessed by the use of class name.
Syntax for creating an object of a static inner class
MainClass .StaticInnerClass objectName = new
MainClass.StaticInnerClass();
16. Example
Write simple example to demonstrate a static member inner class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
17. Facts
â—ŹCompiling any class in java produces a .class file named
ClassName.class
â—ŹCompiling a class with one (or more) inner classes causes both(or
more) classes to be compiled, and produce two(or more) . class files.
E.g ClassName.class and ClassName$innerClassName.class
18. 3. Anonymous inner class
Anonymous inner class is one that is created for implementing an interface or
extending a class. Thereafter, anonymous class must implement an interface or
extend an abstract class but you don’t use keywords extend or implement to
create anonymous class.
Syntax for anonymous inner class
AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
// code
}
};
19. Key facts about anonymous inner class
â–ŞAnonymous inner class cannot have a constructor. Since they have
no name, we can't use them in order to create instances of
anonymous classes. As a result, we have to declare and instantiate
anonymous classes in a single expression at the point of use. We
may either extend an existing class or implement an interface.
â–ŞAnonymous class can access any variables visible to the block
within which anonymous class is declared, including local
variables.
â–ŞAnonymous class can also access methods of a class that contains
it.
20. Example
Write simple example to demonstrate anonymous inner
class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
21. 4. Local inner classes
A local inner class is one that is defined in a block, which is a group of
zero or more statements between balanced braces. You typically find
local classes defined in the body of a method.
22. Example
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;
// method-local inner class
class MethodInner_Demo {
public void print() {
System.out.println("This is method inner class "+num);
}
} // end of inner class
// Accessing the inner class
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.my_Method();
}
}
23. Assignment
Use real life examples of your choice to illustrate the use of
following concepts in java programming;
â—ŹMember inner classes;
â—ŹStatic inner classes
â—ŹAnonymous inner classes
â—ŹLocal inner classes
â—ŹRead about helper classes in Java