Mca(scheme- 2022 ) 2nd sem (Java) module 3
Mca(scheme- 2022 ) 2nd sem (Java) module 3
MODULE 3
Interfaces
Interface fundamentals
• In java, an interface defines a set of methods that will be implemented
by class.
• Interfaces are similar to abstract classes, except that no method can
include a body.
• This means an interface provides no implementation whatsoever of the
methods it defines.
• Once an interface is defined, any number of classes can implement it.
• One class can implement any number of class.
• An interface is defined by use of the interface keyword.
• General form of interface is access
interface name{
ret-type method-name1(param-list);
ret-type method-name2(param-list);
…… ……
ret-type method-nameN(param-list);
}
• Here, access is either public or not used. Implementing an interface
• Once an interface has been defined, one or more classes can implement
the interface.
• To implement an interface, follow these two steps.
1. In a class declaration, include an implements clause that specifies
the interface being implemented.
2. Inside the class, implement the methods defined by the interface.
General form of implements is
class classname implements interface {
//class body
}
Creating an interface
interface printable
{
void print();
}
class A6 implements printable {
public void print() {
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj = new A6(); obj.print();
}
}
Example2
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 TestInterface1 {
public static void main(String args[]) {
Drawable d=new Circle(); d.draw();
}
}
Example3
interface Bank {
int rateOfInterest();
}
class SBI implements Bank {
public int rateOfInterest() {
return 8;
}
}
class PNB implements Bank {
public int rateOfInterest() {
return 9;
}
}
class TestInterface2 {
public static void main(String[] args) {
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}
}
Implementing Multiple Interfaces
If a class implements multiple interfaces, or an interface extends multiple
interfaces i.e. known as multiple inheritance.
Ex:
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();
}
}
class IFExtend {
public static void main(String[] args) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
Constants in Interfaces
• The primary purpose of an interface is to declare methods that provide a
well-defined interface to functionality.
• An interface can also include variables.
• They are implicitly public, static, final and must be initialized.
• In other words, Interface fields are public, static and final by default, and
methods are public and abstract.
• Ex:
interface Const {
int min= 0;
int max = 10;
String error = "Boundary Error";
}
class ConstDemo implements Const {
public static void main(String[] args) {
int[] nums = new int[max];
Nested Interfaces
• An interface can be declared a member of another interface or of a class.
Such an interface is called member interface or a nested interface.
Ex:
interface Showable
{
void show();
interface Message
{
void msg();
}
}
class TestNestedInterface1 implements Showable, Showable.Message {
public void msg() {
System.out.println("Hello nested interface");
}
public void show() {
System.out.println("rnsit");
}
public static void main(String args[]) {
TestNestedInterface1 a=new TestNestedInterface1();
a.msg();
a.show();
}
}
Ex:
interface A
{
void a();
void b();
void c();
void d();
}
abstract class B implements A
{
public void c() {
System.out.println("I am C");
}
}
class M extends B
{
public void a() {
System.out.println("I am a");
}
Packages
• A java package is a group of similar types of classes, interfaces and sub-
packages.
• Package in java can be categorized in two form, builtin package and user
defined package.
• There are many built-in packages such as java, lang, awt, javax, swing,
net, io, util, sql etc.
• All classes in Java belong to some package. When no package has been
explicitly specified, the default (or global) package is used.
• To create a package, you will use the package statement.
• General form of the package statement is package pkg;
• Here pkg is the name of the package.
• More than one file can include the same package statement.
• You can create hierarchy of packages.
• The general form is package pack1.pack2.pack3…..packN;
• Ex: package alpha.beta.gamma;
• must be stored in /alpha/beta/gamma specifies the path to be specified
directories.
• Ex:
package mypack;
public class Simple {
public static void main(String args[]) {
System.out.println("Welcome to package");
}
}
If you want to keep the package within the same directory, you can use .
(dot).
• To compile:
javac -d directory javafilename
• To Run:
java packagename.Simple
• For above program to complile:
javac –d . Simple.java
to run: java mypack.Simple
Access Protection
• Packages act as containers for classes and other subordinate packages.
• Classes act as containers for data and code.
• The class is Java’s smallest unit of abstraction.
• Because of the interplay between classes and packages, Java addresses
four categories of visibility for class members:
• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses.
• The three access specifiers, private, public, and protected, provide a
variety of ways to produce the many levels of access required by these
categories.
*having default access modifier are accessible only within the same
package.
Example
//proC.java
package pack; class proC
{
void msg() {
System.out.println("Hello Bangalore");
}
}
//proD.java
package mypack; import pack.*;
class proD {
public static void main(String args[]) {
proC obj = new proC();
obj.msg();
}
}
output
proD.java:7: error: proC is not public in pack;
cannot be accessed from outside package
proC obj = new proC();
Importing packages
• Using import, you can bring one or more members of a package into
view.
• The general form of import statement. import pkg.classname;
• Here pkg is the name of the package, which can include its full path,
and classname is the name of the class being imported.
• If you want to import the entire contents of a package, use an
asterisk(*) for the class name.
Ex:
import mypack.MyClass;
import mypack.*;
• In the first case,the MyClass is imported from mypack.
• In the second,all of the classes in myback are imported.
EX:
import java.util.Date;
import java.io.*;
• All of the standard Java classes included with Java are stored in a
package called java.
• The basic language functions are stored in a package inside of the java
package called java.lang.
import java.lang.*;
EX:
import java.util.*;
class MyDate extends Date {}
• The same example without the import statement looks like this: class
MyDate extends java.util.Date {}
package pack18;
// packagename.*
public class raghu18
{
public void msg() {
System.out.println("Hello rnsit");
}
}
------------------------------------------------------
package mypack12;
import pack18.*;
class prasad12
{
public static void main(String args[]) {
raghu18 obj = new raghu18();
obj.msg();
}
}
//save by A.java Using packagename.classname
package pack;
public class A {
public void msg(){
System.out.println("Hello");
}
}
--------------------------------------------------------------------------
//save by B.java package mypack; import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
//save by A.java package pack; Using fully qualified name
public class A {
public void msg(){
System.out.println("Hello");
}
}
------------------------------------------------------------------
//save by B.java package mypack;
class B {
public static void main(String args[]) {
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Static import
• The static import feature of Java 5 facilitate the java programmer to
access any static member of a class directly.
• There is no need to qualify it by the class name.
• Static imports are used to save your time and typing.
• If you hate to type same thing again and again then you may find such
imports interesting.
•
Example 1: Without Static Imports
class Demo1 {
public static void main(String args[]) {
double var1= Math.sqrt(5.0); double var2= Math.tan(30);
System.out.println("Square of 5 is:"+ var1);
System.out.println("Tan of 30 is:"+ var2);
}
}
OUTPUT
Square of 5 is:2.236 Tan of 30 is:-6.4053
CLASSPATH
• CLASSPATH describes the location where all the required files are available
which are used in the application.
• Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate
the required files.
• If the CLASSPATH is not set, Java Compiler will not be able to find the
required files and hence will throw the following error.
Error: Could not find or load main class <class name>
Set the CLASSPATH in JAVA in Windows • Command Prompt:
set PATH=.;C:\ProgramFiles\Java\JDK1.6.20\bin
1. Select Start
2. Goto Control Pannel
8.Select OK