SlideShare uma empresa Scribd logo
1 de 31
 OBJECT ORIENTED PROGRAMMING 




         Programming in java
****PACKAGES****


            **AND**




****INTERFACES****


   Programming in java
•   Packages
•   Access Protection
•   Importing packages
•   Java program structure
•   Interfaces
•   Why interface
•   Defining interface
•   Accessing imply thru interface reference
•   Partial Implementations
•   Variables in Interfaces
•   Interfaces Can Be Extended

                Programming in java
Packages
• Till now all java classes were stored in same name
  space.
• As the applications grows it becomes difficult to
  keep all the files in same directory, also we need
  to ensure that the name we choose should be
  unique. To address this problem java introduces a
  mechanism to manage all the classes this is called
  packages
• packages are containers for classes that are
  used to keep the class name space
  compartmentalized.
• It is both a naming and a visibility control
  mechanism
                Programming in java
• To create a package simply include a package
  command as the first statement in a Java source file.
• Any classes declared within that file will belong to
  the specified package .
• The package statement defines a name space in
  which classes are stored.
• If we omit the package statement, the class names
  are put into the default package, which has no name
• General form of the package statement:
  package pkgname;

       Eg: package mypackage;


                   Programming in java
How to run packages

• d:TeachingMCAJavaEclispeWorkBenchTraining
  >cd src

• d:TeachingMCAJavaEclispeWorkBenchTraining
  src>java org.mes.basic.Sample
• Hi

• d:TeachingMCAJavaEclispeWorkBenchTraining
  src>


                Programming in java
How to run..




 Programming in java
• Java uses file system directories to store packages.
• The .class files for any classes you declare to be
  part of mypack must be stored in a directory called
  mypack/ is significant, and the directory name must
  match the package name exactly.
• More than one file can include the same package
  statement.
• The package statement simply specifies to which
  package the classes defined in a file belong. It does
  not exclude other classes in other files from being
  part of that same package.




                  Programming in java
• To create package hierarchy separate each package
  name from the one above it by use of a period.
   package pkg1[.pkg2[.pkg3]];
   package java.awt.image;
• A package hierarchy must be reflected in the file
  system of your Java development system. For
  example, a package declared as
       package    training.mes.mca.inheritance
   needs to be stored in following directory

        trainingmesmcainheritance


                  Programming in java
Access Protection




  Programming in java
Access…
• While Java's access control mechanism may seem
  complicated, we can simplify it as follows.
• Anything declared public can be accessed from
  anywhere. Anything declared private cannot be seen
  outside of its class.
• When a member does not have an explicit access
  specification, it is visible to subclasses as well as to
  other classes in the same package. This is the default
  access.
• If you want to allow an element to be seen outside
  your current package, but only to classes that
  subclass your class directly, then declare that
  element protected. in java
                     Programming
Importing Packages
• In java all of the standard classes are stored in some
  named package.
• If we need use some classes from different package
  we should import that package using import
  statement
• Once imported, a class can be referred to directly,
  using only its name.
• To specify either an explicit classname or a star
  (*), which indicates that the Java compiler should
  import the entire package.
import pkg1[.pkg2].(classname|*);
import java.util.Date;
import java.io.*;
• Refer :TestClassB.java
• /
  Training2/com/mes/training/packageB
  /AccessFromA.javaProgramming in java
Java program structure
• A single package statement (optional)
• Any number of import statements (optional)
• A single public class declaration (required)
• Any number of classes private to the package
  (optional)




                Programming in java
Interfaces
• Using interface, we can fully abstract a class'
  interface from its implementation.
• Using interface, we can specify what a class must
  do, but not how it does it.
• Interfaces are syntactically similar to classes, but
  they lack instance variables, and their methods are
  declared without any body.
• Once it is defined, any number of classes can
  implement an interface.
• One class can implement any number of interfaces.
• To implement an interface, a class must create the
  complete set of methods defined by the interface.
• Each class is free to determine the details of its own
  implementation
                  Programming in java
Why interface?
• Interfaces are designed to support dynamic method
  resolution at run time.
• Normally, in order for a method to be called from one
  class to another, both classes need to be present at
  compile time so the Java compiler can check to ensure
  that the method signatures are compatible. This
  requirement by itself makes for a static and non
  extensible classing environment.
•   Eg:-College Office appln,employee management,Student management both
    needs method to delete insert the employee /student details.So make an
    interface which has all the CURD ops and implement that is all classes




                        Programming in java
Defining interface
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
• Access can be public or default
• The methods which are declared have no
  bodies(abstract). They end with a semicolon after the
  parameter list.
• Each class that includes an interface must implement all
  of the methods.
                   Programming in java
• Variables can be declared inside of interface
  declarations. They are implicitly final and static,
  meaning they cannot be changed by the implementing
  class.
• Variables must also be initialized with a constant
  value. All methods and variables are implicitly public
  if the interface, itself, is declared as public.
  interface Callback {
        void callback(int param);
  }

• Once an interface has been defined, one or more
  classes can implement that interface


                  Programming in java
• To implement an interface, include the
  implements clause in a class definition, and then
  create the methods defined by the interface
  access class classname [extends superclass]
  [implements interface [,interface...]] {
  // class-body
  }
• access is either public or not used.
• If a class implements more than one interface , the
  interfaces are separated with a comma.
• If a class implements two interfaces that declare the
  same method, then the same method will be used by
  clients of either interface.



                   Programming in java
• The methods that implement an interface must be
  declared public.
• Also, the type signature of the implementing method
  must match exactly the type signature specified in
  the interface definition
   interface Callback {
   void callback(int param);
   }
   class Client implements Callback {
       public void callback(int p) {
  System.out.println("callback called
  with " + p);
  }
  }
                  Programming in java
interface Callback {
void callback(int param);
}
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called
  with " + p);
}
}
• It is both permissible and common for classes that
  implement interfaces to define additional members
  of their own.

                  Programming in java
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called
  with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that
  implement interfaces " +
"may also define other members, too.");
}
}


             Programming in java
Accessing implementation through interface
               references

• It is possible to declare variables as object references
  that use an interface rather than a class type.
• Any instance of any class that implements the
  declared interface can be stored in such a variable.
  When you call a method through one of these
  references, the correct version will be called based
  on the actual instance of the interface being referred
  to. This is one of the key features of interfaces.




                  Programming in java
• The method to be executed is looked up dynamically
  at run time, allowing classes to be created later than
  the code which calls methods on them.
• The calling code can dispatch through an interface
  without having to know anything about the "callee."
  This process is similar to using a superclass
  reference
   to access a subclass object, as described in
  DynamicMethodDespatch




                   Programming in java
interface Callback {
void callback(int param);
}
class Client implements Callback {
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement
  interfaces " +
"may also define other members, too.");
}
}
class TestIface {
public static void main(String args[]) {
Callback c = new Client();
c.callback(42);
}
}
callback called with 42
                    Programming in java
• Notice that variable c is declared to be of the
  interface type Callback, yet it was assigned an
  instance of Client. Although c can be used to access
  the callback( ) method, it cannot access any other
  members of the Client class.
• An interface reference variable only has knowledge
  of the methods declared by its interface declaration.
  Thus, c could not be used to access nonIfaceMeth( )
  since it is defined by Client but not Callback




                   Programming in java
Partial Implementations
• If a class includes an interface but does not fully
  implement the methods defined by that interface, then
  that class must be declared as abstract.
abstract class Incomplete implements
  Callback {
int a, b;
void show() {
System.out.println(a + " " + b);
}
// ...
}
• Here, the class Incomplete does not implement
  callback() and must be declared as abstract.
• Any class that inherits Incomplete must implement
  callback() or be declared abstract itself.

                 Programming in java
Variables in Interfaces

• You can use interfaces to import shared constants
  into multiple classes by simply declaring an
  interface that contains variables which are initialized
  to the desired values.
• If an interface contains no methods, then any class
  that includes such an interface doesn't actually
  implement anything. It is as if that class were
  importing the constant variables into the class name
  space as final variables.
• Ref: SharedConstAppln.java



                   Programming in java
Interfaces Can Be Extended


• One interface can inherit another by use of the
  keyword extends. The syntax is the same as for
  inheriting classes.
• When a class implements an interface that inherits
  another interface, it must provide implementations
  for all methods defined within the interface
  inheritance chain




                  Programming in java
Reference
• /
  Training2/com/mes/training/interfaces/SharedConst
  Appln.java
• /
  Training/src/org/mes/inheritance/SalutationApplicati
  on.java
• /
  Training2/com/mes/training/interfaces/InterfaceAppl
  n.java




                  Programming in java
SUBMITTED

                  -S.SANTHOSH KUMAR




Programming in java
THANK
               YOU…!




Programming in java

Mais conteúdo relacionado

Mais procurados (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java packages
Java packagesJava packages
Java packages
 
java Features
java Featuresjava Features
java Features
 
Java exception
Java exception Java exception
Java exception
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java program structure
Java program structure Java program structure
Java program structure
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Operators in java
Operators in javaOperators in java
Operators in java
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Java swing
Java swingJava swing
Java swing
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

Destaque

5.interface and packages
5.interface and packages5.interface and packages
5.interface and packagesDeepak Sharma
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
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
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & PackagesArindam Ghosh
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 

Destaque (20)

Interface
InterfaceInterface
Interface
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
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 multi threading
Java multi threadingJava multi threading
Java multi threading
 
Java packages
Java packagesJava packages
Java packages
 
java packages
java packagesjava packages
java packages
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Java interface
Java interfaceJava interface
Java interface
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 

Semelhante a Packages and interfaces

Semelhante a Packages and interfaces (20)

Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Packages
PackagesPackages
Packages
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Understanding Annotations in Java
Understanding Annotations in JavaUnderstanding Annotations in Java
Understanding Annotations in Java
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Java Programming Fundamentals
Java Programming Fundamentals Java Programming Fundamentals
Java Programming Fundamentals
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Java features
Java featuresJava features
Java features
 
Java
JavaJava
Java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Java notes
Java notesJava notes
Java notes
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
 
Learn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat ShahriyarLearn Java with Dr. Rifat Shahriyar
Learn Java with Dr. Rifat Shahriyar
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Core Java Introduction | Basics
Core Java Introduction  | BasicsCore Java Introduction  | Basics
Core Java Introduction | Basics
 

Packages and interfaces

  • 1.  OBJECT ORIENTED PROGRAMMING  Programming in java
  • 2. ****PACKAGES**** **AND** ****INTERFACES**** Programming in java
  • 3. Packages • Access Protection • Importing packages • Java program structure • Interfaces • Why interface • Defining interface • Accessing imply thru interface reference • Partial Implementations • Variables in Interfaces • Interfaces Can Be Extended Programming in java
  • 4. Packages • Till now all java classes were stored in same name space. • As the applications grows it becomes difficult to keep all the files in same directory, also we need to ensure that the name we choose should be unique. To address this problem java introduces a mechanism to manage all the classes this is called packages • packages are containers for classes that are used to keep the class name space compartmentalized. • It is both a naming and a visibility control mechanism Programming in java
  • 5. • To create a package simply include a package command as the first statement in a Java source file. • Any classes declared within that file will belong to the specified package . • The package statement defines a name space in which classes are stored. • If we omit the package statement, the class names are put into the default package, which has no name • General form of the package statement: package pkgname; Eg: package mypackage; Programming in java
  • 6. How to run packages • d:TeachingMCAJavaEclispeWorkBenchTraining >cd src • d:TeachingMCAJavaEclispeWorkBenchTraining src>java org.mes.basic.Sample • Hi • d:TeachingMCAJavaEclispeWorkBenchTraining src> Programming in java
  • 7. How to run.. Programming in java
  • 8. • Java uses file system directories to store packages. • The .class files for any classes you declare to be part of mypack must be stored in a directory called mypack/ is significant, and the directory name must match the package name exactly. • More than one file can include the same package statement. • The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package. Programming in java
  • 9. • To create package hierarchy separate each package name from the one above it by use of a period. package pkg1[.pkg2[.pkg3]]; package java.awt.image; • A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as package training.mes.mca.inheritance needs to be stored in following directory trainingmesmcainheritance Programming in java
  • 10. Access Protection Programming in java
  • 11. Access… • While Java's access control mechanism may seem complicated, we can simplify it as follows. • Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. • When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. • If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected. in java Programming
  • 12. Importing Packages • In java all of the standard classes are stored in some named package. • If we need use some classes from different package we should import that package using import statement • Once imported, a class can be referred to directly, using only its name. • To specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. import pkg1[.pkg2].(classname|*); import java.util.Date; import java.io.*; • Refer :TestClassB.java • / Training2/com/mes/training/packageB /AccessFromA.javaProgramming in java
  • 13. Java program structure • A single package statement (optional) • Any number of import statements (optional) • A single public class declaration (required) • Any number of classes private to the package (optional) Programming in java
  • 14. Interfaces • Using interface, we can fully abstract a class' interface from its implementation. • Using interface, we can specify what a class must do, but not how it does it. • Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. • Once it is defined, any number of classes can implement an interface. • One class can implement any number of interfaces. • To implement an interface, a class must create the complete set of methods defined by the interface. • Each class is free to determine the details of its own implementation Programming in java
  • 15. Why interface? • Interfaces are designed to support dynamic method resolution at run time. • Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. This requirement by itself makes for a static and non extensible classing environment. • Eg:-College Office appln,employee management,Student management both needs method to delete insert the employee /student details.So make an interface which has all the CURD ops and implement that is all classes Programming in java
  • 16. Defining interface access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; } • Access can be public or default • The methods which are declared have no bodies(abstract). They end with a semicolon after the parameter list. • Each class that includes an interface must implement all of the methods. Programming in java
  • 17. • Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. • Variables must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public. interface Callback { void callback(int param); } • Once an interface has been defined, one or more classes can implement that interface Programming in java
  • 18. • To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface access class classname [extends superclass] [implements interface [,interface...]] { // class-body } • access is either public or not used. • If a class implements more than one interface , the interfaces are separated with a comma. • If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. Programming in java
  • 19. • The methods that implement an interface must be declared public. • Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition interface Callback { void callback(int param); } class Client implements Callback { public void callback(int p) { System.out.println("callback called with " + p); } } Programming in java
  • 20. interface Callback { void callback(int param); } class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } } • It is both permissible and common for classes that implement interfaces to define additional members of their own. Programming in java
  • 21. class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces " + "may also define other members, too."); } } Programming in java
  • 22. Accessing implementation through interface references • It is possible to declare variables as object references that use an interface rather than a class type. • Any instance of any class that implements the declared interface can be stored in such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. Programming in java
  • 23. • The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. • The calling code can dispatch through an interface without having to know anything about the "callee." This process is similar to using a superclass reference to access a subclass object, as described in DynamicMethodDespatch Programming in java
  • 24. interface Callback { void callback(int param); } class Client implements Callback { public void callback(int p) { System.out.println("callback called with " + p); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces " + "may also define other members, too."); } } class TestIface { public static void main(String args[]) { Callback c = new Client(); c.callback(42); } } callback called with 42 Programming in java
  • 25. • Notice that variable c is declared to be of the interface type Callback, yet it was assigned an instance of Client. Although c can be used to access the callback( ) method, it cannot access any other members of the Client class. • An interface reference variable only has knowledge of the methods declared by its interface declaration. Thus, c could not be used to access nonIfaceMeth( ) since it is defined by Client but not Callback Programming in java
  • 26. Partial Implementations • If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract. abstract class Incomplete implements Callback { int a, b; void show() { System.out.println(a + " " + b); } // ... } • Here, the class Incomplete does not implement callback() and must be declared as abstract. • Any class that inherits Incomplete must implement callback() or be declared abstract itself. Programming in java
  • 27. Variables in Interfaces • You can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values. • If an interface contains no methods, then any class that includes such an interface doesn't actually implement anything. It is as if that class were importing the constant variables into the class name space as final variables. • Ref: SharedConstAppln.java Programming in java
  • 28. Interfaces Can Be Extended • One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. • When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain Programming in java
  • 29. Reference • / Training2/com/mes/training/interfaces/SharedConst Appln.java • / Training/src/org/mes/inheritance/SalutationApplicati on.java • / Training2/com/mes/training/interfaces/InterfaceAppl n.java Programming in java
  • 30. SUBMITTED -S.SANTHOSH KUMAR Programming in java
  • 31. THANK YOU…! Programming in java