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

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Inner Class
• Packages

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");
}

// A static inner class


static class Inner {
public static void main(String[] args) {
System.out.println("inside inner class Method");
outerMethod();
}
}
}
PACKAGES

Package in Java is a mechanism to encapsulate a group of classes, sub packages


and interfaces.
Packages are used for:
• Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college.staff.cse.Employee and
college.staff.ee.Employee.
• Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
• Providing controlled access: protected and default have package level access control.
A protected member is accessible by classes in the same package and its subclasses.
A default member is accessible by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).
PACKAGES

How packages work?


• Package names and directory structure are closely related. For example if a
package name is college.staff.cse, then there are three directories, college, staff
and cse such that cse is present in staff and staff is present college.
• Also, the directory college is accessible through CLASSPATH variable, i.e.,
path of parent directory of college is present in CLASSPATH. The idea is to make
sure that classes are easy to locate.
Package naming conventions :
• Packages are named in reverse order of domain names. For example, in a college,
the recommended convention is college.tech.cse, college.tech.ee,
college.art.history, etc.
PACKAGES
Adding a class to a Package :
We can add more classes to a created package by using package name at the
top of the program and saving it in the package directory. We need a new java
file to define a public class, otherwise we can add the new class to an
existing .java file and recompile it.

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

• These are the packages that are defined by the user.


• First we create a directory myPackage (name should be same as the name of
the package).
• Then create the MyClass inside the directory with the first statement being the
package names.
PACKAGES
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.*
• If you N use package.* then all the classes and interfaces of this package will be accessible
but not subpackages.
• The import keyword is used to make the classes and interface of another package accessible to
the current package.
PACKAGES
Using packagename.classname
• If you import package.classname then only declared class of this package will be
accessible.

Using fully qualified name


• If you use fully qualified name then only declared class of this package will be
accessible.
• Now there is no need to import. But you need to use fully qualified name every
• time when you are accessing the class or interface.
• It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
PACKAGES

SYNTAX:

// Name of the package must be same as the directory


// under which this file is saved

package myPackage;

public class MyClass


{
public void getNames(String s)
{
System.out.println(s);
}
}
PACKAGES
EXAMPLE:
Now we can use the MyClass class in our program.
/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;

public class PrintName


{
public static void main(String args[])
{
// Initializing the String variable
// with a value
String name = "GeeksforGeeks";
// Creating an instance of class MyClass in
// the package.
MyClass obj = new MyClass();
obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage directory since it is a part of the package.
QUIZ:

Ques 1. Which is true about an anonymous inner class?

A. It can extend exactly one class and implement


exactly one interface.

B. It can extend exactly one class and can implement multiple


interfaces.

C. It can extend exactly one class or implement exactly one


interface.

D. It can implement multiple interfaces regardless of whether it


also extends a class
QUIZ:
Ques 2: Which of the following is an incorrect statement
about packages?

a) Package defines a namespace in which classes are stored

b) A package can contain other package within it

c) Java uses file system directories to store packages

d) A package can be renamed without renaming the


directory in which the classes are stored
Summary:

In this session, you were able to :


• Learn about inner classes and its types.
• Understand packages with example
References
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw
Hill Companies
3. John P. Flynt Thomson, Java Programming.

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

You might also like