0% found this document useful (0 votes)
5 views

Mca(scheme- 2022 ) 2nd sem (Java) module 3

This document covers the concepts of interfaces and packages in Java, detailing how interfaces define methods for classes to implement, including examples of creating and using interfaces. It also explains the structure and usage of Java packages, including access protection, importing packages, and static imports. Additionally, it contrasts abstract classes with interfaces, highlighting their differences and use cases.

Uploaded by

shivuhunji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Mca(scheme- 2022 ) 2nd sem (Java) module 3

This document covers the concepts of interfaces and packages in Java, detailing how interfaces define methods for classes to implement, including examples of creating and using interfaces. It also explains the structure and usage of Java packages, including access protection, importing packages, and static imports. Additionally, it contrasts abstract classes with interfaces, highlighting their differences and use cases.

Uploaded by

shivuhunji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming Using Java 22MCA22

MODULE 3

INTERFACES AND PACKAGES

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();
}
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |1


Object Oriented Programming Using Java 22MCA22

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();
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |2


Object Oriented Programming Using Java 22MCA22

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();
}
}

Interfaces can be extended


• One interface can be inherit another by use of the keyword extends.
• The syntax is same as for inheriting classes.
• When a class implements an interface that inherits another interface.
• It must be provide implementations for all
• methods defined within the interface inheritance chain.
• Ex:
interface A
{
void meth1();
void meth2();
}
interface B extends A
{
void meth3();
}
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1");
}
public void meth2() {
System.out.println("Implement meth2");
}
public void meth3() {
System.out.println("Implement meth3");
}
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |3


Object Oriented Programming Using Java 22MCA22

class IFExtend {
public static void main(String[] args) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

USING INTERFACE REFERENCES


• When you create a class, you are creating a new reference type.
• The same is true of interefaces.
• An interface declaration also creates a new reference type.
• When a class implements an interface, it is adding that interface's type to
its type.
• As a result, an instance of a class that implements an interface is also an
instance of that interface type.
EX:
interface Printable {
void sysout();
void displayName();
}
class Parent implements Printable {
public void displayName() {
System.out.println("JAVA CLASS");
}
public void sysout() {
System.out.println("MCA");
}
}
public class interface10 {
public static void main(String[] args) {
Parent obj=new Parent(); Printable a;
a=obj;
a.sysout();
a.displayName();
}
}

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.

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |4


Object Oriented Programming Using Java 22MCA22

• 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];

for(int i=min; i < (max + 1); i++)


{
if(i >= max)
System.out.println(error);
else {
nums[i] = i; System.out.print(nums[i] + " ");
}
}
}
}

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();
}
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |5


Object Oriented Programming Using Java 22MCA22

Abstract Class vs Interface

Abstract class Interface


Abstract class can Interface can have only
have abstract and abstract methods.
nonabstract methods.
Abstract class doesn't Interface supports
support multiple multiple inheritance.
inheritance.
Abstract class can Interface has only
have final, non-final, static and final
static and non-static variables.
variables.
Abstract class can Interface can't have
have static methods, static methods, main
main method and method or
constructor. constructor.
Abstract class can Interface can't provide
provide the the implementation
implementation of of abstract class.
interface.
The abstract keyword The interface keyword
is used to declare is used to declare
abstract class. interface.

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");
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |6


Object Oriented Programming Using Java 22MCA22

public void b() {


System.out.println("I am b");
}
public void d() {
System.out.println("I am d");
}
}
class Test5 {
public static void main(String args[])
{
A a=new M();
a.a();
a.b();
a.c();
a.d();
}
}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |7


Object Oriented Programming Using Java 22MCA22

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

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |8


Object Oriented Programming Using Java 22MCA22

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.

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT Page |9


Object Oriented Programming Using Java 22MCA22

Class member access

*having default access modifier are accessible only within the same
package.

protected access modifier


• The protected access modifier is accessible within package and outside
the package but through inheritance only.
• The protected access modifier can be applied on the data member,
method and constructor. It can't be applied on the class.
Ex:
//save by proA.java
package pack; public class proA {
protected void msg() {
System.out.println("WELCOME TO JAVA");
}
}
//save by proB.java
package mypack; import pack.*;
class proB extends proA {
public static void main(String args[])
{
proB obj = new proB();
obj.msg();
}
}

default access modifier


• If you don't use any modifier, it is treated as default by default.
• The default modifier is accessible only within
package. It cannot be accessed from outside the package.

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 10


Object Oriented Programming Using Java 22MCA22

• It provides more accessibility than private. But, it is more restrictive than


protected, and public.

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 {}

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 11


Object Oriented Programming Using Java 22MCA22

• The same example without the import statement looks like this: class
MyDate extends java.util.Date {}

Importing java’s Standard Packages


• Java defines a large number of standard classes that are available to all
programs.
Subpackage Description
java.lang Contains a large
number of general-
purpose classes
java.io Contains the I/O
classes
java.net Contains those classes
that support
networking
java.Applet Contains classes for
creating applets
java.awt Contains classes that
support the Abstract
Window Toolkit
java.util Contains various utility
classes, plus the
Collections Framework.

How to access package from another package?


There are three ways to access the package from outside the package.
import package.*;
import package.classname; fully qualified name.

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

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 12


Object Oriented Programming Using Java 22MCA22

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

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 13


Object Oriented Programming Using Java 22MCA22

Example 2: Using Static Imports

import static java.lang.System.*;


import static java.lang.Math.*;
class Demo2 {
public static void main(String args[]) {
double var1= sqrt(5.0); double var2= tan(30);
out.println("Square of 5 is:"+var1);
out.println("Tan of 30 is:"+var2);
}
}

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

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 14


Object Oriented Programming Using Java 22MCA22

3. Select System and Security

4.Select Advanced System Settings

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 15


Object Oriented Programming Using Java 22MCA22

5. Click on environment variable

6.Click on new under system variable

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 16


Object Oriented Programming Using Java 22MCA22

7. Add CLASSPATH as variable name and path of file

8.Select OK

Mr. Raghu Prasad K, Assistant Professor, MCA Dept | RNSIT P a g e | 17

You might also like