SlideShare a Scribd company logo
INTERFACE AND 
PACKAGES 
BCA 5th Sem 
DEEPAK SHARMA 
12KSSB6031
INTERFACE 
 In the JAVA programming language, an Interface is 
a reference type, similar to a class, that can contain only 
constants, method signatures and nested types. There 
are no method bodies. Interfaces cannot be instantiated, 
they can only be implemented by classes or extended 
by other Interfaces. 
 Interfaces are used to code similarities which 
classes of various type share, but do not necessarily 
constitute a class relationship.
DEFINITION 
Interface use for implementing multiple inheritance 
Syntax of interface 
interface interfacename 
{ 
staticfinal variables; 
abstract method; 
} 
Interface consists staticfinal variable and abstract methods. 
Eg: interface i1 
{ 
int i=5; 
Float k=27.5; 
void meth1(); // Abstract method 
}
EXTENDING INTERFACE 
 An interface can extend another interface, similarly to the 
way that a class can extend another class. 
 The extends keyword is used to extend an interface, 
and the child interface inherits the methods of the parent 
interface. 
 Interface can be sub interfaced from other interfaces. 
 A sub interface will inherit all members of super interface.
VARIOUS FORM OF IMPLEMENTING 
INTERFACE 
interface A 
implements 
class B 
C 
extends 
class 
A 
interface 
class class 
B C 
implements
A 
B 
C 
D 
E 
class 
extends 
class 
extends 
class 
implements 
interface 
extends 
interface 
interface 
A B 
extends 
C 
class D 
interface 
interface 
implements
EXAMPLE INTERFACE PROGRAM 
 public interface Area 
 { 
 final float pi=3.142; 
 float compute(float x, float y); 
 } 
 public class Circle implements Area 
 { 
 public float compute(float x, float y) 
 { 
 return(pi*x*x); 
 } 
 } 
 public class Rectangle implements Area 
 { 
 public float compute(float x, float y) 
 { 
 return(x*y); 
 } 
 } 
Area 
Compute 
Circle Rectangle 
Compute 
implements 
Compute
PACKAGES 
DEFINITION: 
 A Package is a logical organization of related classes. 
The java classes can be logically organized into 
packages. 
 Packages is a collection of classes and interface.
USER DEFINED PACKAGES 
 JAVA uses file system to manage packages, with each package 
stored in its own directory. 
 Package names are case sensitive. The directory name and 
the package name should be the same. 
 The classes declared within the package directory will belong to 
the specified package.
HIDING CLASSES 
The main advantage of packages is to hide the classes 
from other packages, if we declare the class as public, 
then the class is visible in all packages. If we do not 
want the class to be public, the default access is used. 
1. 
package pack1; 
public class ABC 
{ 
-------; 
} 
2. 
package pack1; 
public class ABC{ 
} 
class XYZ{ 
}
WHY DO WE NEED PACKAGES? 
• One can easily determine that these types are 
related. 
• One knows where to find types that can provide 
task-related functions. 
• The names of your types won't conflict with the 
type names in other packages because the 
package creates a new namespace. 
• One can allow types within the package to have 
unrestricted access to one another yet still restrict 
access for types outside the package.
ACCESS TO MEMBERS OF THE 
CLASSES 
private (default) protected public 
The same class Yes Yes Yes Yes 
Subclass in the package No Yes Yes Yes 
Non-subclass in the package No Yes Yes Yes 
Subclass in another package No No Yes Yes 
Non-subclass in another package No No No Yes
ACCESS MODIFIER 
package p1 
class C1 
public int x 
class C3 
C1 c1; 
c1.x can be read or 
modified 
package p2 
class C2 extends C1 
x can be read or 
modified in C2 
class C4 
C1 c1; 
c1.x can be read nor 
modified
THANK YOU

More Related Content

What's hot

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Burhan Ahmed
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
polymorphism
polymorphism polymorphism
polymorphism
Imtiaz Hussain
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
SanjayKumarMahto1
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 
Packages in java
Packages in javaPackages in java
Packages in java
Jerlin Sundari
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Maliha Mehr
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
foreverredpb
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 

What's hot (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Packages in java
Packages in javaPackages in java
Packages in java
 
polymorphism
polymorphism polymorphism
polymorphism
 
File in C language
File in C languageFile in C language
File in C language
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Java threads
Java threadsJava threads
Java threads
 

Viewers also liked

Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
vanithaRamasamy
 
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
dheeraj_cse
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
Deepak Sharma
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
Java packages
Java packagesJava packages
Java packages
BHUVIJAYAVELU
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
9.cs instrset
9.cs instrset9.cs instrset
9.cs instrset
Deepak Sharma
 
1.sdlc
1.sdlc1.sdlc
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
java packages
java packagesjava packages
java packages
aptechsravan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
2.software requirement specification
2.software requirement specification2.software requirement specification
2.software requirement specification
Deepak Sharma
 
10.data transmission
10.data transmission10.data transmission
10.data transmission
Deepak Sharma
 
8.flip flops and registers
8.flip flops and registers8.flip flops and registers
8.flip flops and registers
Deepak Sharma
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Java interface
Java interfaceJava interface
Java interface
Carlos Pereira
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Kwangshin Oh
 

Viewers also liked (20)

Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
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
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java package
Java packageJava package
Java package
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Interface
InterfaceInterface
Interface
 
9.cs instrset
9.cs instrset9.cs instrset
9.cs instrset
 
1.sdlc
1.sdlc1.sdlc
1.sdlc
 
Java packages
Java packagesJava packages
Java packages
 
Java interface
Java interfaceJava interface
Java interface
 
java packages
java packagesjava packages
java packages
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
2.software requirement specification
2.software requirement specification2.software requirement specification
2.software requirement specification
 
10.data transmission
10.data transmission10.data transmission
10.data transmission
 
8.flip flops and registers
8.flip flops and registers8.flip flops and registers
8.flip flops and registers
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java interface
Java interfaceJava interface
Java interface
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
 

Similar to 5.interface and packages

Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
arnold 7490
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
Mavoori Soshmitha
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
Narayana Swamy
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
GayathriRHICETCSESTA
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
Arindam Ghosh
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Interface &packages
Interface &packagesInterface &packages
Interface &packages
Shah Ishtiyaq Mehfooze
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
Andhra University
 
javapackage
javapackagejavapackage
javapackage
Arjun Shanka
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
SanthiNivas
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
mrecedu
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Java 06
Java 06Java 06
Java 06
Loida Igama
 
Packages
PackagesPackages
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 

Similar to 5.interface and packages (20)

Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
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 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Interface &packages
Interface &packagesInterface &packages
Interface &packages
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
javapackage
javapackagejavapackage
javapackage
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Java 06
Java 06Java 06
Java 06
 
Packages
PackagesPackages
Packages
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 

5.interface and packages

  • 1. INTERFACE AND PACKAGES BCA 5th Sem DEEPAK SHARMA 12KSSB6031
  • 2. INTERFACE  In the JAVA programming language, an Interface is a reference type, similar to a class, that can contain only constants, method signatures and nested types. There are no method bodies. Interfaces cannot be instantiated, they can only be implemented by classes or extended by other Interfaces.  Interfaces are used to code similarities which classes of various type share, but do not necessarily constitute a class relationship.
  • 3. DEFINITION Interface use for implementing multiple inheritance Syntax of interface interface interfacename { staticfinal variables; abstract method; } Interface consists staticfinal variable and abstract methods. Eg: interface i1 { int i=5; Float k=27.5; void meth1(); // Abstract method }
  • 4. EXTENDING INTERFACE  An interface can extend another interface, similarly to the way that a class can extend another class.  The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.  Interface can be sub interfaced from other interfaces.  A sub interface will inherit all members of super interface.
  • 5. VARIOUS FORM OF IMPLEMENTING INTERFACE interface A implements class B C extends class A interface class class B C implements
  • 6. A B C D E class extends class extends class implements interface extends interface interface A B extends C class D interface interface implements
  • 7. EXAMPLE INTERFACE PROGRAM  public interface Area  {  final float pi=3.142;  float compute(float x, float y);  }  public class Circle implements Area  {  public float compute(float x, float y)  {  return(pi*x*x);  }  }  public class Rectangle implements Area  {  public float compute(float x, float y)  {  return(x*y);  }  } Area Compute Circle Rectangle Compute implements Compute
  • 8. PACKAGES DEFINITION:  A Package is a logical organization of related classes. The java classes can be logically organized into packages.  Packages is a collection of classes and interface.
  • 9. USER DEFINED PACKAGES  JAVA uses file system to manage packages, with each package stored in its own directory.  Package names are case sensitive. The directory name and the package name should be the same.  The classes declared within the package directory will belong to the specified package.
  • 10. HIDING CLASSES The main advantage of packages is to hide the classes from other packages, if we declare the class as public, then the class is visible in all packages. If we do not want the class to be public, the default access is used. 1. package pack1; public class ABC { -------; } 2. package pack1; public class ABC{ } class XYZ{ }
  • 11. WHY DO WE NEED PACKAGES? • One can easily determine that these types are related. • One knows where to find types that can provide task-related functions. • The names of your types won't conflict with the type names in other packages because the package creates a new namespace. • One can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.
  • 12. ACCESS TO MEMBERS OF THE CLASSES private (default) protected public The same class Yes Yes Yes Yes Subclass in the package No Yes Yes Yes Non-subclass in the package No Yes Yes Yes Subclass in another package No No Yes Yes Non-subclass in another package No No No Yes
  • 13. ACCESS MODIFIER package p1 class C1 public int x class C3 C1 c1; c1.x can be read or modified package p2 class C2 extends C1 x can be read or modified in C2 class C4 C1 c1; c1.x can be read nor modified