SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
JAVA concepts
Packages and interfaces
Exception handling
Accessing
classes from
package
Naming
conventions
API
packages
Creating
packages
Packages and interfaces
Packages
Packages are java’s way of grouping a variety of classes and
interfaces together.
Packages acts as “containers ” for classes.
Benefits of using packages:
Reused
Hiding
separating “design” from “coding”.
Two classes in two different packages can have same name.
packages
Packages and interfaces
Java
API
Packages
Java API provides a large number of classes grouped into
different packages according to functionality.
Packages and interfaces
Package name Contents
java.lang Language support classes that java compiler itself uses and
automatically imported . They include classes for primitive types ,
strings , math functions , threads and exceptions.
java.util Language utility classes such as vectors , hash tables , random
numbers , date , etc.
java.io Input/output support classes that provide facilities for input and
output of data.
java.awt Set of classes for implementing GUI like windows , buttons , lists ,
menus , colors , etc.
java.net Classes for networking for communication with local computer as
well as with internet servers
java.applet Classes for creating and implementing applets
Packages and interfaces
Using
system
Packages
jav
a
Package
Containing
awt package
Package
Containing
classes
Classes
Containing
methods
Hierarchical representation of
java.awt package
There are two ways of accessing the classes stored in the package.
Java.awt.Color;
import packagename.classname;
(or)
import packagename.*;
First approach is perhaps easy and best way to access a class only
once or when we need not have to access any other classes of the
package.
But, in many situations we want to use a class in number of places
in program then we use second approach
Packages and interfaces
Accessing
the
classes
stored in
the
package
There are known as import statements and must appear at the top
of the file , before any class declarations , import is a keyword.
import Java.awt.Color;
This statement allows specified class in specified package to
be imported
import java.awt.*;
This statement imports every class contained in the specified
package.
Packages and interfaces
Accessing
the
classes
stored in
the
package
Naming
conventio
ns
Packages begin with lowercase .
java.lang.Math.sqrt(x);
Packages and interfaces
Package
name
Class
name
Method
name
Packages and interfaces
CLICK HERE FOR MORE INFO
Creating packages
We must first declare the name of the package using package
keyword followed by package name.
This must be the first statement in the source file except the
comments and whitespaces followed by normal class definition.
package firstPackage; //package declaration
public class firstclass //class definition
{
………….
…………..
…………..
}
Packages and interfaces
CLICK HERE FOR MORE INFO
Creating packages
Remember that the .class files must be located in a directory that has the same
name as the package and this directory should be a subdirectory where classes
will import the package are located.
Creating ore own packages involve the following steps:
Declare the package at the beginning of the file using the form
package packagename;
Define the class that is to be put in the package and declare it public.
Create the subdirectory under the directory where the main source files are
stored.
Store the listing as the class name java file in the subdirectory created.
Compile the file . This creates .class file in the subdirectory
Packages and interfaces
Accessing a package
import package1 [.package2] [.package3].classname;
Packages and interfaces
Adding a class to a package
It is simple to add a class to an existing package.
package p1;
public class A
{
//body of A
}
if we want to add another class B to this package
Define the class and make it public
Place the package statements
package p1;
public class B{
//body of B }
Store B.java file in p1 directory.
Compile B.java file . This will create B.class file and place it in the directory
p1.
0
Implementing
interface
Naming
conventions
Extending
interface
Creating
packages
Packages and interfaces
Why do we need Interfaces?
For the use of multiple inheritance which is not supported by
Java
A java class cannot be a subclass of more than one super class , it
can implement more than one interface , enabling us to create
classes that build upon other classes without the problem created
by multiple inheritance.
Interface
Packages and interfaces
Defining
interfaces
An interface is
basically a kind
of class
What is the difference between
an interface and a class?
Interface defines only abstract methods and fields. I .e. , the
interface do not specify any code to implement these methods
and data fields contain only constants
Therefore it is the responsibility of the class that implements an
interface to define the code for implementation of these methods.
Packages and interfaces
Syntax
for
interface
interface interfacename
{
variables declaration;
methods declaration;
}
Here interface is the keyword .
Example :
interface area
{
final static float pi=3.14f;
Float compute(float x , float y)
void show();
}
Like classes , interfaces can also be extended by the key word
extends
Syntax :
interface name2 extends name1{
//Body of name2}
Example :
interface ItemConstants
{
int code = 1001;
String name=“fan”;
}
Interafce Item extends Item Constans
{
void display();
}
Packages and interfaces
Extending
interfaces
Impleme
nting
interfaces
Interfaces are used as “ superclasses ” whose properties
are inherited by classes.
It is necessary to create a class that inherits the given
interface.
Syntax:
class classname implements interfacename
{
//body of class
}
Here class implements the interface. implements is the
keyword
If a class that implements an interface does not
implement all the methods of the interface then the class
becomes an abstract class and cannot be instantiated
Packages and interfaces
Impleme
nting
interface
with an
example
interface Area
{
final static float pi=3.14f;
float compute (float x , float y );
}
Class Rectangle implements Area
{
public float compute(float x, float y)
{
Return(x*y);
}
}
class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class InterfaceTest
Packages and interfaces
Packages and interfaces
Various Forms of interface
implementation
nterface
mplementation
class
Extension
class
class
class
class
Extension
Extension
interface
interface
implementation
Extension
Various Forms of interface
implementation
nterface
implementation
class
class class
Extension
interface interface
implementation
Accessing
interface
variable
Packages and interfaces
Interfaces can be used to declare a set of constants that can be
used in different classes
This is similar to creating header files in C++
The constant class will be available to any class that implements
the interface . The values can be used in any method.
Ex : interface A{
int m=10;
Int n=50;}
Class B implements A
{
int x=m;
void methodB ( int size
{
………..
If (size<n)
……….
)
Thank
you

More Related Content

PPS
Packages and inbuilt classes of java
PPT
exception handling in java.ppt
PPT
Packages in java
PPTX
Java features
PPT
Java buzzwords
PPTX
Java string handling
PPTX
Two-dimensional array in java
Packages and inbuilt classes of java
exception handling in java.ppt
Packages in java
Java features
Java buzzwords
Java string handling
Two-dimensional array in java

What's hot (20)

PPTX
Constructor in java
PPT
9. Input Output in java
PPTX
Applets in java
PPTX
Java - Generic programming
PPTX
Exception handling
PPTX
Input output files in java
PPT
Visual Basic 6.0
PPTX
Packages,static,this keyword in java
PPTX
Type casting in java
PPTX
Strings in Java
PPSX
Exception Handling
PPTX
Java swing
PDF
Wrapper classes
PPTX
Java Program Structure
PDF
Java Fundamentals
PPTX
Java package
PPTX
Multi-threaded Programming in JAVA
PPT
Java Notes
PPT
java swing
Constructor in java
9. Input Output in java
Applets in java
Java - Generic programming
Exception handling
Input output files in java
Visual Basic 6.0
Packages,static,this keyword in java
Type casting in java
Strings in Java
Exception Handling
Java swing
Wrapper classes
Java Program Structure
Java Fundamentals
Java package
Multi-threaded Programming in JAVA
Java Notes
java swing
Ad

Viewers also liked (20)

PPTX
Chap1 packages
PPTX
Exception Handling in Java
PPTX
Java packages oop
PPT
exception handling
PPTX
Java packages
PDF
Java - Interfaces & Packages
PPT
Java interface
PPTX
Java - Exception Handling
PDF
Java Exception Handling Best Practices - Improved Second Version
PPTX
5.interface and packages
PPT
java packages
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
Java exception handling
PPTX
Exception handling in Java
PPTX
Java packages
ODP
Exception Handling In Java
PDF
Java exception handling ppt
PPTX
Exception handling
PPT
Packages and interfaces
PPS
Java Exception handling
Chap1 packages
Exception Handling in Java
Java packages oop
exception handling
Java packages
Java - Interfaces & Packages
Java interface
Java - Exception Handling
Java Exception Handling Best Practices - Improved Second Version
5.interface and packages
java packages
Interface in java By Dheeraj Kumar Singh
Java exception handling
Exception handling in Java
Java packages
Exception Handling In Java
Java exception handling ppt
Exception handling
Packages and interfaces
Java Exception handling
Ad

Similar to Packages,interfaces and exceptions (20)

PPTX
Z blue interfaces and packages (37129912)
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PPTX
java package in java.. in java packages.
PPTX
java package java package in java packages
PPT
PACKAGE.PPT12345678912345949745654646455
PPT
Unit 4 Java
PPTX
Packages and interface
DOCX
Unit4 java
PPTX
packages in java object oriented programming
PPTX
Package in Java
DOCX
Class notes(week 7) on packages
PPTX
PDF
Unit 2 notes.pdf
PDF
Java packages
PPTX
Packages in java
PDF
Class notes(week 7) on packages
PPT
packages.ppt
PPTX
Package.pptx
PDF
Java - Packages Concepts
PPTX
packages in java & c++
Z blue interfaces and packages (37129912)
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
java package in java.. in java packages.
java package java package in java packages
PACKAGE.PPT12345678912345949745654646455
Unit 4 Java
Packages and interface
Unit4 java
packages in java object oriented programming
Package in Java
Class notes(week 7) on packages
Unit 2 notes.pdf
Java packages
Packages in java
Class notes(week 7) on packages
packages.ppt
Package.pptx
Java - Packages Concepts
packages in java & c++

More from Mavoori Soshmitha (6)

PPTX
Ppt on java basics1
PPTX
Multi threading
PPTX
Inheritance
PPT
main memory
PPT
Virtual memory
PPTX
Ppt on java basics
Ppt on java basics1
Multi threading
Inheritance
main memory
Virtual memory
Ppt on java basics

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
A Presentation on Artificial Intelligence
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MYSQL Presentation for SQL database connectivity
A Presentation on Artificial Intelligence
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Unlocking AI with Model Context Protocol (MCP)

Packages,interfaces and exceptions

  • 1. JAVA concepts Packages and interfaces Exception handling
  • 2. Accessing classes from package Naming conventions API packages Creating packages Packages and interfaces Packages Packages are java’s way of grouping a variety of classes and interfaces together. Packages acts as “containers ” for classes. Benefits of using packages: Reused Hiding separating “design” from “coding”. Two classes in two different packages can have same name. packages
  • 3. Packages and interfaces Java API Packages Java API provides a large number of classes grouped into different packages according to functionality.
  • 4. Packages and interfaces Package name Contents java.lang Language support classes that java compiler itself uses and automatically imported . They include classes for primitive types , strings , math functions , threads and exceptions. java.util Language utility classes such as vectors , hash tables , random numbers , date , etc. java.io Input/output support classes that provide facilities for input and output of data. java.awt Set of classes for implementing GUI like windows , buttons , lists , menus , colors , etc. java.net Classes for networking for communication with local computer as well as with internet servers java.applet Classes for creating and implementing applets
  • 5. Packages and interfaces Using system Packages jav a Package Containing awt package Package Containing classes Classes Containing methods Hierarchical representation of java.awt package
  • 6. There are two ways of accessing the classes stored in the package. Java.awt.Color; import packagename.classname; (or) import packagename.*; First approach is perhaps easy and best way to access a class only once or when we need not have to access any other classes of the package. But, in many situations we want to use a class in number of places in program then we use second approach Packages and interfaces Accessing the classes stored in the package
  • 7. There are known as import statements and must appear at the top of the file , before any class declarations , import is a keyword. import Java.awt.Color; This statement allows specified class in specified package to be imported import java.awt.*; This statement imports every class contained in the specified package. Packages and interfaces Accessing the classes stored in the package
  • 8. Naming conventio ns Packages begin with lowercase . java.lang.Math.sqrt(x); Packages and interfaces Package name Class name Method name
  • 9. Packages and interfaces CLICK HERE FOR MORE INFO Creating packages We must first declare the name of the package using package keyword followed by package name. This must be the first statement in the source file except the comments and whitespaces followed by normal class definition. package firstPackage; //package declaration public class firstclass //class definition { …………. ………….. ………….. }
  • 10. Packages and interfaces CLICK HERE FOR MORE INFO Creating packages Remember that the .class files must be located in a directory that has the same name as the package and this directory should be a subdirectory where classes will import the package are located. Creating ore own packages involve the following steps: Declare the package at the beginning of the file using the form package packagename; Define the class that is to be put in the package and declare it public. Create the subdirectory under the directory where the main source files are stored. Store the listing as the class name java file in the subdirectory created. Compile the file . This creates .class file in the subdirectory
  • 11. Packages and interfaces Accessing a package import package1 [.package2] [.package3].classname;
  • 12. Packages and interfaces Adding a class to a package It is simple to add a class to an existing package. package p1; public class A { //body of A } if we want to add another class B to this package Define the class and make it public Place the package statements package p1; public class B{ //body of B } Store B.java file in p1 directory. Compile B.java file . This will create B.class file and place it in the directory p1. 0
  • 13. Implementing interface Naming conventions Extending interface Creating packages Packages and interfaces Why do we need Interfaces? For the use of multiple inheritance which is not supported by Java A java class cannot be a subclass of more than one super class , it can implement more than one interface , enabling us to create classes that build upon other classes without the problem created by multiple inheritance. Interface
  • 14. Packages and interfaces Defining interfaces An interface is basically a kind of class What is the difference between an interface and a class? Interface defines only abstract methods and fields. I .e. , the interface do not specify any code to implement these methods and data fields contain only constants Therefore it is the responsibility of the class that implements an interface to define the code for implementation of these methods.
  • 15. Packages and interfaces Syntax for interface interface interfacename { variables declaration; methods declaration; } Here interface is the keyword . Example : interface area { final static float pi=3.14f; Float compute(float x , float y) void show(); }
  • 16. Like classes , interfaces can also be extended by the key word extends Syntax : interface name2 extends name1{ //Body of name2} Example : interface ItemConstants { int code = 1001; String name=“fan”; } Interafce Item extends Item Constans { void display(); } Packages and interfaces Extending interfaces
  • 17. Impleme nting interfaces Interfaces are used as “ superclasses ” whose properties are inherited by classes. It is necessary to create a class that inherits the given interface. Syntax: class classname implements interfacename { //body of class } Here class implements the interface. implements is the keyword If a class that implements an interface does not implement all the methods of the interface then the class becomes an abstract class and cannot be instantiated Packages and interfaces
  • 18. Impleme nting interface with an example interface Area { final static float pi=3.14f; float compute (float x , float y ); } Class Rectangle implements Area { public float compute(float x, float y) { Return(x*y); } } class Circle implements Area { public float compute(float x, float y) { return(pi*x*x); } } class InterfaceTest Packages and interfaces
  • 20. Various Forms of interface implementation nterface mplementation class Extension class class class class Extension Extension interface interface implementation Extension
  • 21. Various Forms of interface implementation nterface implementation class class class Extension interface interface implementation
  • 22. Accessing interface variable Packages and interfaces Interfaces can be used to declare a set of constants that can be used in different classes This is similar to creating header files in C++ The constant class will be available to any class that implements the interface . The values can be used in any method. Ex : interface A{ int m=10; Int n=50;} Class B implements A { int x=m; void methodB ( int size { ……….. If (size<n) ………. )