Professional Documents
Culture Documents
Lecture-2.2
Lecture-2.2
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes
2
INNER CLASS
Inner class means one class which is a member of another class.
There are basically four types of inner classes in java.
1) Nested Inner class
2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
NESTED INNER CLASS
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.
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();
}}
METHOD LOCAL INNER CLASS
Inner class can be declared within a method of an outer class.
In the following example, Inner is an inner class in outerMethod().
class Outer {
void outerMethod() {
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
System.out.println("inside innerMethod");
}}
Inner y = new Inner();
y.innerMethod();
}}
ANONYMOUS INNER CLASS
Anonymous inner classes are declared without any name at all.
class Demo {
void show() {
System.out.println("i am in show method of super class");
}}
class Flavor1Demo {
// An anonymous class with Demo as base class
static Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in Flavor1Demo class");
} };
public static void main(String[] args){
d.show();
}}
STATIC NESTED INNER CLASS
Static nested classes are not technically an inner class. They are like a
static member of outer class.
class Outer {
private static void outerMethod() {
System.out.println("inside outerMethod");
}
Subpackages:
Packages that are inside another package are the subpackages. These are not
imported by default, they have to imported explicitly. Also, members of a
subpackage have no access privileges, i.e., they are considered as different
package for protected and default access specifiers.
Example : import java.util.*;
util is a subpackage created inside java package.
PACKAGES
Built-in Packages
These packages consist of a large number of classes which are a part of J Java API. Some of the
commonly used built-in packages are:
1)java.lang: Contains language support classes(e.g classed which defines primitive data types,
math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3)java.util: Contains utility classes which implement data structures like Linked List, Dictionary
and support ; for Date / Time operations.
4) java. Applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , menus etc.).
6) java.net: Contain classes for supporting networking operations.
PACKAGES
User-defined packages
SYNTAX:
package myPackage;
Video Lectures :
https://www.youtube.com/watch?v=V7yVbG9_xkM
E-book :
https://www.tutorialspoint.com/java/java_innerclasses.htm
https://www.geeksforgeeks.org/packages-in-java/
THANK YOU