1
INTRODUCTION TO INTERFACE • An interface in java is a blueprint of a class. • It has static constants and abstract methods. • The interface in java is a mechanism to achieve abstraction. • There can be only abstract methods in the java interface not method body. • It cannot be instantiated just like abstract class. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces. 2
DECLARING INTERFACE • The interface keyword is used to declare an interface. • Syntax: public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations } 3
interface • An interface is implicitly abstract. • So not need to use the abstract keyword while declaring an interface. • Each Fields in an interface is also implicitly static and final, so the static and final keyword is not needed (Refer below diagram). • Methods in an interface are also implicitly public. 4
Interface example Example: Interface ItemConstants //interface declared { int code = 1001; // Variable declared in interface string name = “Fan”; void display(); // Method declared in interface } 5
Extending interface • An interface can extend another interface like class. • The extends keyword is used to extend an interface. • A class implements an interface. interface Name2 extends Name1 { Body of Name2 } 6
implementing interface • Interfaces are used as “Super classes” whose properties are inherited by classes. • syntax Class className implements interfacename { Body of classname } 7
implementing interface Example: interface Drawable // Interface declared { void draw(); } class Rectangle implements Drawable //implementing { public void draw() { System.out.println("drawing rectangle"); }} OUTPUT: drawing rectangle 8
implementing interface interface Printable { void print(); } interface Showable { void show(); } class A7 implements Printable,Showable { public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); } public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); } } OUTPUT: Hello Welcome 9
10
• A java package is a group of similar types of classes, interfaces and sub-packages. • Package in java can be categorized in two types, 1. Java API package (Built-in package) 2. User-defined package. (Defined by user) • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. • Java package provides access protection. 11
• Java APl(Application Program Interface) provides a large numbers of classes grouped into different packages according to functionality. • Most of the time we use the packages available with the Java API. 12
PACKAGE CONTENTS java.lang Language support classes. They include classes for primitive types, string, math functions, thread and exceptions. java.util Language utility classes such as vectors, hash tables, random numbers, data, etc. java.io Input/output support classes. They provide facilities for the input and output of data. java.applet Classes for creating and implementing applets. java.net Classes for networking. They include classes for communicating with local computers as well as with internet servers. java.awt Set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on. 13
• The import statements are used at the top of the file, before any class declarations. • The first statement allows the specified class in the specified package to be imported. • For example, Import java.awt.color; • The above statement imports class color and therefore the class name can now be directly used in the program. • The below statement imports every class contained in the specified package. Import java.awt.*; • The above statement will bring all classes of java.awt package. 14
• To create a package, a name should be selected for the package. • Include a package statement along with that name at the top of every source file that contains the classes, interfaces. • The package statement should be the first line in the source file. • There can be only one package statement in each source file, and it applies to all types in the file. 15
//save as Simple.java package mypack; // Package name public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } } 16
To compile and run the package, To Compile: javac -d . Simple.java To Run: java mypack.Simple • The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. • The . (dot) represents the current folder. OUTPUT: Welcome to package 17
ACCESSING A PACKAGE: There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 18
//save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } } Output: Hello //save by B.java package mypack; import pack.*; //Package.* class B { public static void main(args[]) { A obj = new A(); obj.msg(); }} 19
//save by A.java package pack; public class A { public void msg() { System.out.println("Hello"); } } Output: Hello //save by B.java package mypack; import pack.A; //Package.classname class B { public static void main(args[]) { A obj = new A(); obj.msg(); }} 20
• Using fully qualified name can declared a class of this package will be accessible. • Now there is no need to import. • But you need to use fully qualified name every time when you are accessing the class or interface. • It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. 21
//save by A.java package pack; public class A{ public void msg() { System.out.println("Hello"); }} //save by B.java package mypack; class B { public static void main(args[]) { pack.A obj = new pack.A(); (Fully qualified name) obj.msg(); } } 22
23

java interface and packages

  • 1.
  • 2.
    INTRODUCTION TO INTERFACE •An interface in java is a blueprint of a class. • It has static constants and abstract methods. • The interface in java is a mechanism to achieve abstraction. • There can be only abstract methods in the java interface not method body. • It cannot be instantiated just like abstract class. • An interface is not extended by a class; it is implemented by a class. • An interface can extend multiple interfaces. 2
  • 3.
    DECLARING INTERFACE • Theinterface keyword is used to declare an interface. • Syntax: public interface NameOfInterface { // Any number of final, static fields // Any number of abstract method declarations } 3
  • 4.
    interface • An interfaceis implicitly abstract. • So not need to use the abstract keyword while declaring an interface. • Each Fields in an interface is also implicitly static and final, so the static and final keyword is not needed (Refer below diagram). • Methods in an interface are also implicitly public. 4
  • 5.
    Interface example Example: Interface ItemConstants//interface declared { int code = 1001; // Variable declared in interface string name = “Fan”; void display(); // Method declared in interface } 5
  • 6.
    Extending interface • Aninterface can extend another interface like class. • The extends keyword is used to extend an interface. • A class implements an interface. interface Name2 extends Name1 { Body of Name2 } 6
  • 7.
    implementing interface • Interfacesare used as “Super classes” whose properties are inherited by classes. • syntax Class className implements interfacename { Body of classname } 7
  • 8.
    implementing interface Example: interface Drawable// Interface declared { void draw(); } class Rectangle implements Drawable //implementing { public void draw() { System.out.println("drawing rectangle"); }} OUTPUT: drawing rectangle 8
  • 9.
    implementing interface interface Printable { voidprint(); } interface Showable { void show(); } class A7 implements Printable,Showable { public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); } public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); } } OUTPUT: Hello Welcome 9
  • 10.
  • 11.
    • A javapackage is a group of similar types of classes, interfaces and sub-packages. • Package in java can be categorized in two types, 1. Java API package (Built-in package) 2. User-defined package. (Defined by user) • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. • Java package provides access protection. 11
  • 12.
    • Java APl(ApplicationProgram Interface) provides a large numbers of classes grouped into different packages according to functionality. • Most of the time we use the packages available with the Java API. 12
  • 13.
    PACKAGE CONTENTS java.lang Languagesupport classes. They include classes for primitive types, string, math functions, thread and exceptions. java.util Language utility classes such as vectors, hash tables, random numbers, data, etc. java.io Input/output support classes. They provide facilities for the input and output of data. java.applet Classes for creating and implementing applets. java.net Classes for networking. They include classes for communicating with local computers as well as with internet servers. java.awt Set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on. 13
  • 14.
    • The importstatements are used at the top of the file, before any class declarations. • The first statement allows the specified class in the specified package to be imported. • For example, Import java.awt.color; • The above statement imports class color and therefore the class name can now be directly used in the program. • The below statement imports every class contained in the specified package. Import java.awt.*; • The above statement will bring all classes of java.awt package. 14
  • 15.
    • To createa package, a name should be selected for the package. • Include a package statement along with that name at the top of every source file that contains the classes, interfaces. • The package statement should be the first line in the source file. • There can be only one package statement in each source file, and it applies to all types in the file. 15
  • 16.
    //save as Simple.java packagemypack; // Package name public class Simple { public static void main(String args[]) { System.out.println("Welcome to package"); } } 16
  • 17.
    To compile andrun the package, To Compile: javac -d . Simple.java To Run: java mypack.Simple • The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. • The . (dot) represents the current folder. OUTPUT: Welcome to package 17
  • 18.
    ACCESSING A PACKAGE: Thereare three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 18
  • 19.
    //save by A.java packagepack; public class A { public void msg() { System.out.println("Hello"); } } Output: Hello //save by B.java package mypack; import pack.*; //Package.* class B { public static void main(args[]) { A obj = new A(); obj.msg(); }} 19
  • 20.
    //save by A.java packagepack; public class A { public void msg() { System.out.println("Hello"); } } Output: Hello //save by B.java package mypack; import pack.A; //Package.classname class B { public static void main(args[]) { A obj = new A(); obj.msg(); }} 20
  • 21.
    • Using fullyqualified name can declared a class of this package will be accessible. • Now there is no need to import. • But you need to use fully qualified name every time when you are accessing the class or interface. • It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. 21
  • 22.
    //save by A.java packagepack; public class A{ public void msg() { System.out.println("Hello"); }} //save by B.java package mypack; class B { public static void main(args[]) { pack.A obj = new pack.A(); (Fully qualified name) obj.msg(); } } 22
  • 23.