SlideShare a Scribd company logo
1 of 23
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

What's hot

Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programmingDamian T. Gordon
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and SetsNicole Ryan
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
5 collection framework
5 collection framework5 collection framework
5 collection frameworkMinal Maniar
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
 

What's hot (20)

Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Packages in java
Packages in javaPackages in java
Packages in java
 
STL in C++
STL in C++STL in C++
STL in C++
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Enumeration in c#
Enumeration in c#Enumeration in c#
Enumeration in c#
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
This pointer
This pointerThis pointer
This pointer
 
Java swing
Java swingJava swing
Java swing
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 

Viewers also liked

Chap1 packages
Chap1 packagesChap1 packages
Chap1 packagesraksharao
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & PackagesArindam Ghosh
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception HandlingPrabhdeep Singh
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionLemi Orhan Ergin
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packagesDeepak Sharma
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaPrasad Sawant
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 

Viewers also liked (20)

Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
exception handling
exception handlingexception handling
exception handling
 
Java packages
Java packagesJava packages
Java packages
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Java interface
Java interfaceJava interface
Java interface
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Java package
Java packageJava package
Java package
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second Version
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
java packages
java packagesjava packages
java packages
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java packages
Java packagesJava packages
Java packages
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 

Similar to Packages,interfaces and exceptions

Similar to Packages,interfaces and exceptions (20)

Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Packages
PackagesPackages
Packages
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Java packags
Java packagsJava packags
Java packags
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 

More from Mavoori Soshmitha

More from Mavoori Soshmitha (6)

Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Inheritance
InheritanceInheritance
Inheritance
 
main memory
main memorymain memory
main memory
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Ppt on java basics
Ppt on java basicsPpt on java basics
Ppt on java basics
 

Recently uploaded

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

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) ………. )