SlideShare a Scribd company logo
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 same name in two packages,
• Makes 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 (without any
access specifier) is accessible by classes in the same package only.
• Packages that are inside another package are the subpackages. These
are not imported by default, they have to imported explicitly.
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
User-defined packages
These are the packages that are defined by the user. First we create a
directory mypack . Then create the class Simple inside the directory with the first
statement being the package names.
//save as Simple.java
package mypack;
class SimplePackage{
public void display(){
System.out.println("Welcome to package");
}
}
import mypack.SimplePackage;
public class PackageUse
{
public static void main(String args[])
{
SimplePackage p=new SimplePackage();
p.display();
}
}
To compile java package:
javac -d . Simple.java // -d specifies the destination where to put the
generated class file, to keep the package within the same directory use . (dot).
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Unit3 part3-packages and interfaces
• It is a collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface.
• Interface cannot be instantiated.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a class.
• An interface can extend multiple interfaces.
• Interface methods do not have a body - the body is provided by the
"implement" class
• On implementation of an interface, you must override all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and final
• To achieve security - hide certain details and only show the important details
of an object
• "multiple inheritance" can be achieved with interfaces, because the class
can implement multiple interfaces.
INTERFACES IN JAVA
interface <interface_name>{
// declare constant fields
// declare methods that abstract by default.
}
The Java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data
members.
a class extends another class, an interface extends another interface, but
a class implements an interface.
A class can extend only one class, but implement many interfaces..
interface In1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
class InterfaceTestClass implements In1
{
// Implementing the capabilities of interface.
public void display()
{
System.out.println(“Kristu Jayanti College");
}
public static void main (String[] args)
{
InterfaceTestClass t = new InterfaceTestClass();
t.display();
System.out.println(a);
}
interface Drawable{
void draw();
}
class Rectangle implements Drawable{
public void draw()
{System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw()
{System.out.println("drawing circle");}
}
class InterfaceTest1{
public static void main(String args[]){
Drawable d=new Circle();
d.draw();
}}
• A class can implement more than one interface at a time.
• A class can extend only one class, but implement many interfaces.
• An interface can extend another interface, in a similar way as a class can
extend another class.
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text from irst interface..");
}
public void myOtherMethod() {
System.out.println("Some text from second interface...");
}
}
class MulInterfaceTest1 {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
interface Printable{
void print();
}
interface Showable{
void show();
}
class MultipleInheritance implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
MultipleInheritance obj = new MultipleInheritance();
obj.print();
obj.show();
}
}
• Multiple inheritance is not supported through class in java, but it is possible by
an interface, why?
• multiple inheritance is not supported in the case of class because of ambiguity.
However, it is supported in case of an interface because there is no ambiguity.
It is because its implementation is provided by the implementation class.
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface3 implementss Printable, Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
obj.print();
}
}
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class InterfaceInheritance implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
InterfaceInheritance obj = new InterfaceInheritance();
obj.print();
obj.show();
}
}
INTERFACE INHERITANCE
Provides classes that are fundamental to the design of the Java programming
language.
Object, the ultimate superclass of all classes in Java
• Thread, the class that controls each thread in a multithreaded program
• Throwable, the superclass of all error and exception classes in Java
• Classes that encapsulate the primitive data types in Java
• Classes for accessing system resources and other low-level entities
• Math, a class that provides standard mathematical methods
• String, the class that is used to represent strings
Java.lang Package In Java
• Wrapper classes are those whose objects wraps a primitive data type within
them.
• In the java.lang package java provides a separate class for each of the
primitive data types namely Byte, Character, Double, Integer, Float, Long,
Short.
• At the time of instantiation, these classes accept a primitive datatype
directly, and wrap a primitive value into a wrapper class object.
• They convert primitive data types into objects.
Unit3 part3-packages and interfaces
• It contains the utility classes (a string tokenizer, a random-number generator, and a bit
array).
• AbstractCollection: This class provides a skeletal implementation of the Collection
interface, to minimize the effort required to implement this interface.
• Arrays: This class contains various methods for manipulating arrays (such as sorting and
searching).
• Calendar: The Calendar class is an abstract class that provides methods for converting
between a specific instant in time and a set of calendar fields such as YEAR, MONTH,
DAY_OF_MONTH, HOUR,
• Currency: Represents a currency.
• Date: The class Date represents a specific instant in time, with millisecond precision.
• Random: An instance of this class is used to generate a stream of pseudorandom
numbers.
• Scanner: A simple text scanner which can parse primitive types and strings using regular
expressions.
• String Tokenizer: The string tokenizer class allows an application to break a string into
tokens.
• Treeset: A NavigableSet implementation based on a TreeMap.
java.util PACKAGE
• This package provides for system input and output through data streams,
serialization and the file system.
• Java uses the concept of a stream to make I/O operation fast. The java.io
package contains all the classes required for input and output operations.
3 streams are created automatically. All these streams are attached with the
console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
JAVA.IO PACKAGE IN JAVA
Java.io.FileInputStream Class in Java
FileInputStream is useful to read data from a file in the form of sequence of
bytes.For reading streams of characters, consider using FileReader.
Constructor and Description
FileInputStream(File file) :Creates an input file stream to read from the
specified File object.
FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read
from the specified file descriptor.
FileInputStream(String name) :Creates an input file stream to read from a file
with the specified name.
Important Methods:
int read() : Reads a byte of data from this input stream
int read(byte[] b) :Reads up to b.length bytes of data from this input stream into
an array of bytes.
int read(byte[] b, int off, int len) : Reads up to len bytes of data from this input
stream into an array of bytes.
void close() : Closes this file input stream and releases any system resources
associated with the stream.
• FileOutputStream is meant for writing streams of raw bytes
• It can be used to create text files. A file represents storage of data on a
second storage media like a hard disk or CD. Whether or not a file is
available or may be created
void close() : Closes this file output stream and releases any system resources
associated with this stream.
protected void finalize() : Cleans up the connection to the file, and ensures
that the close method of this file output stream is called when there are no
more references to this stream.
void write(byte[] b) : Writes b.length bytes from the specified byte array to
this file output stream.
void write(byte[] b, int off, int len) : Writes len bytes from the specified
byte array starting at offset off to this file output stream.
void write(int b) : Writes the specified byte to this file output stream.

More Related Content

PPTX
Unit 5 java-awt (1)
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
PPTX
Unit3 packages &amp; interfaces
PPTX
Introduction to java
PPTX
Packages and interfaces
PPTX
Unit3 part1-class
PPTX
Unit3 part2-inheritance
PPTX
Packages and Interfaces
Unit 5 java-awt (1)
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit3 packages &amp; interfaces
Introduction to java
Packages and interfaces
Unit3 part1-class
Unit3 part2-inheritance
Packages and Interfaces

What's hot (20)

PPTX
Pi j4.1 packages
PDF
Java packages
PPTX
Object oriented programming in java
PPTX
Unit 4 exceptions and threads
DOCX
Class notes(week 7) on packages
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Week10 packages using objects in objects
PPTX
Unit3 inheritance
PPT
packages and interfaces
PPTX
Java class,object,method introduction
PPT
Object and Classes in Java
PPTX
Inner classes in java
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
PPTX
Multiple inheritance possible in Java
PPTX
Pi j3.2 polymorphism
PPTX
Inheritance in Java
PDF
Object Oriented Programming in PHP
PPTX
java interface and packages
PPT
Java: Inheritance
Pi j4.1 packages
Java packages
Object oriented programming in java
Unit 4 exceptions and threads
Class notes(week 7) on packages
JAVA PROGRAMMING – Packages - Stream based I/O
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Week10 packages using objects in objects
Unit3 inheritance
packages and interfaces
Java class,object,method introduction
Object and Classes in Java
Inner classes in java
Lecture - 2 Environment setup & JDK, JRE, JVM
Multiple inheritance possible in Java
Pi j3.2 polymorphism
Inheritance in Java
Object Oriented Programming in PHP
java interface and packages
Java: Inheritance
Ad

Similar to Unit3 part3-packages and interfaces (20)

PPTX
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
ODP
Synapseindia reviews.odp.
PPTX
object oriented programming using java, second sem BCA,UoM
PPTX
Objects and classes in OO Programming concepts
PPT
Java tutorials
PDF
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
PDF
Java programming basics
PPTX
Java-Intro.pptx
PPTX
Module 1.pptx
PPTX
Java For beginners and CSIT and IT students
PPTX
packages in java object oriented programming
PPTX
Classes, Inheritance ,Packages & Interfaces.pptx
PPT
Lec8_Java Advanced class features_Part 1V.ppt
PPT
Jacarashed-1746968053-300050282-Java.ppt
PPT
Inheritance
PPT
Java Tutorial 1
PDF
PPTX
C# classes objects
PPTX
Interfaces in java
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
Synapseindia reviews.odp.
object oriented programming using java, second sem BCA,UoM
Objects and classes in OO Programming concepts
Java tutorials
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
Java programming basics
Java-Intro.pptx
Module 1.pptx
Java For beginners and CSIT and IT students
packages in java object oriented programming
Classes, Inheritance ,Packages & Interfaces.pptx
Lec8_Java Advanced class features_Part 1V.ppt
Jacarashed-1746968053-300050282-Java.ppt
Inheritance
Java Tutorial 1
C# classes objects
Interfaces in java
Ad

More from DevaKumari Vijay (16)

PPTX
Unit 1 computer architecture (1)
PPTX
PPTX
PPTX
PPTX
Unit 2 monte carlo simulation
PPTX
Decisiontree&amp;game theory
PPTX
Unit2 network optimization
PPTX
Unit 4 simulation and queing theory(m/m/1)
PPTX
Unit4 systemdynamics
PPTX
Unit 3 des
PPTX
Unit 1 introduction to simulation
PPTX
Unit2 montecarlosimulation
PPT
Unit 3-Greedy Method
PPTX
Unit1 introduction to Java
PPT
Introduction to design and analysis of algorithm
PPTX
Operations research lpp
Unit 1 computer architecture (1)
Unit 2 monte carlo simulation
Decisiontree&amp;game theory
Unit2 network optimization
Unit 4 simulation and queing theory(m/m/1)
Unit4 systemdynamics
Unit 3 des
Unit 1 introduction to simulation
Unit2 montecarlosimulation
Unit 3-Greedy Method
Unit1 introduction to Java
Introduction to design and analysis of algorithm
Operations research lpp

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .
102 student loan defaulters named and shamed – Is someone you know on the list?
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Renaissance Architecture: A Journey from Faith to Humanism
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025

Unit3 part3-packages and interfaces

  • 2. • 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 same name in two packages, • Makes 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 (without any access specifier) is accessible by classes in the same package only. • Packages that are inside another package are the subpackages. These are not imported by default, they have to imported explicitly.
  • 5. User-defined packages These are the packages that are defined by the user. First we create a directory mypack . Then create the class Simple inside the directory with the first statement being the package names. //save as Simple.java package mypack; class SimplePackage{ public void display(){ System.out.println("Welcome to package"); } }
  • 6. import mypack.SimplePackage; public class PackageUse { public static void main(String args[]) { SimplePackage p=new SimplePackage(); p.display(); } } To compile java package: javac -d . Simple.java // -d specifies the destination where to put the generated class file, to keep the package within the same directory use . (dot). To Compile: javac -d . Simple.java To Run: java mypack.Simple
  • 8. • It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. • Interface cannot be instantiated. • An interface does not contain any constructors. • All of the methods in an interface are abstract. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces. • Interface methods do not have a body - the body is provided by the "implement" class • On implementation of an interface, you must override all of its methods • Interface methods are by default abstract and public • Interface attributes are by default public, static and final • To achieve security - hide certain details and only show the important details of an object • "multiple inheritance" can be achieved with interfaces, because the class can implement multiple interfaces. INTERFACES IN JAVA
  • 9. interface <interface_name>{ // declare constant fields // declare methods that abstract by default. } The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members.
  • 10. a class extends another class, an interface extends another interface, but a class implements an interface. A class can extend only one class, but implement many interfaces..
  • 11. interface In1 { // public, static and final final int a = 10; // public and abstract void display(); } class InterfaceTestClass implements In1 { // Implementing the capabilities of interface. public void display() { System.out.println(“Kristu Jayanti College"); } public static void main (String[] args) { InterfaceTestClass t = new InterfaceTestClass(); t.display(); System.out.println(a); }
  • 12. interface Drawable{ void draw(); } class Rectangle implements Drawable{ public void draw() {System.out.println("drawing rectangle");} } class Circle implements Drawable{ public void draw() {System.out.println("drawing circle");} } class InterfaceTest1{ public static void main(String args[]){ Drawable d=new Circle(); d.draw(); }}
  • 13. • A class can implement more than one interface at a time. • A class can extend only one class, but implement many interfaces. • An interface can extend another interface, in a similar way as a class can extend another class. • If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
  • 14. interface FirstInterface { public void myMethod(); // interface method } interface SecondInterface { public void myOtherMethod(); // interface method } class DemoClass implements FirstInterface, SecondInterface { public void myMethod() { System.out.println("Some text from irst interface.."); } public void myOtherMethod() { System.out.println("Some text from second interface..."); } } class MulInterfaceTest1 { public static void main(String[] args) { DemoClass myObj = new DemoClass(); myObj.myMethod(); myObj.myOtherMethod(); } }
  • 15. interface Printable{ void print(); } interface Showable{ void show(); } class MultipleInheritance implements Printable,Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ MultipleInheritance obj = new MultipleInheritance(); obj.print(); obj.show(); } }
  • 16. • Multiple inheritance is not supported through class in java, but it is possible by an interface, why? • multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class. interface Printable{ void print(); } interface Showable{ void print(); } class TestInterface3 implementss Printable, Showable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ TestInterface3 obj = new TestInterface3(); obj.print(); } }
  • 17. interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class InterfaceInheritance implements Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ InterfaceInheritance obj = new InterfaceInheritance(); obj.print(); obj.show(); } } INTERFACE INHERITANCE
  • 18. Provides classes that are fundamental to the design of the Java programming language. Object, the ultimate superclass of all classes in Java • Thread, the class that controls each thread in a multithreaded program • Throwable, the superclass of all error and exception classes in Java • Classes that encapsulate the primitive data types in Java • Classes for accessing system resources and other low-level entities • Math, a class that provides standard mathematical methods • String, the class that is used to represent strings Java.lang Package In Java
  • 19. • Wrapper classes are those whose objects wraps a primitive data type within them. • In the java.lang package java provides a separate class for each of the primitive data types namely Byte, Character, Double, Integer, Float, Long, Short. • At the time of instantiation, these classes accept a primitive datatype directly, and wrap a primitive value into a wrapper class object. • They convert primitive data types into objects.
  • 21. • It contains the utility classes (a string tokenizer, a random-number generator, and a bit array). • AbstractCollection: This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface. • Arrays: This class contains various methods for manipulating arrays (such as sorting and searching). • Calendar: The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, • Currency: Represents a currency. • Date: The class Date represents a specific instant in time, with millisecond precision. • Random: An instance of this class is used to generate a stream of pseudorandom numbers. • Scanner: A simple text scanner which can parse primitive types and strings using regular expressions. • String Tokenizer: The string tokenizer class allows an application to break a string into tokens. • Treeset: A NavigableSet implementation based on a TreeMap. java.util PACKAGE
  • 22. • This package provides for system input and output through data streams, serialization and the file system. • Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations. 3 streams are created automatically. All these streams are attached with the console. 1) System.out: standard output stream 2) System.in: standard input stream 3) System.err: standard error stream JAVA.IO PACKAGE IN JAVA
  • 23. Java.io.FileInputStream Class in Java FileInputStream is useful to read data from a file in the form of sequence of bytes.For reading streams of characters, consider using FileReader. Constructor and Description FileInputStream(File file) :Creates an input file stream to read from the specified File object. FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read from the specified file descriptor. FileInputStream(String name) :Creates an input file stream to read from a file with the specified name. Important Methods: int read() : Reads a byte of data from this input stream int read(byte[] b) :Reads up to b.length bytes of data from this input stream into an array of bytes. int read(byte[] b, int off, int len) : Reads up to len bytes of data from this input stream into an array of bytes. void close() : Closes this file input stream and releases any system resources associated with the stream.
  • 24. • FileOutputStream is meant for writing streams of raw bytes • It can be used to create text files. A file represents storage of data on a second storage media like a hard disk or CD. Whether or not a file is available or may be created void close() : Closes this file output stream and releases any system resources associated with this stream. protected void finalize() : Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream. void write(byte[] b) : Writes b.length bytes from the specified byte array to this file output stream. void write(byte[] b, int off, int len) : Writes len bytes from the specified byte array starting at offset off to this file output stream. void write(int b) : Writes the specified byte to this file output stream.