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

Unit V Interface and Package

The document discusses interfaces and packages in Java. It defines an interface as a collection of method signatures without implementations that can be used to achieve abstraction and multiple inheritance. Classes can implement multiple interfaces. Packages are used to organize Java code and provide access protection. The key differences between abstract classes and interfaces are that interfaces contain only abstract methods while abstract classes can contain concrete methods, and interfaces support multiple inheritance while abstract classes do not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Unit V Interface and Package

The document discusses interfaces and packages in Java. It defines an interface as a collection of method signatures without implementations that can be used to achieve abstraction and multiple inheritance. Classes can implement multiple interfaces. Packages are used to organize Java code and provide access protection. The key differences between abstract classes and interfaces are that interfaces contain only abstract methods while abstract classes can contain concrete methods, and interfaces support multiple inheritance while abstract classes do not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit V: Interfaces and Packages(4 Hrs)

Defining Interfaces, Interfaces vs. Classes, Extending Interfaces, Implementing


Interfaces, Multiple Inheritance by using interfaces, Abstract Classes vs. Interfaces.
Importance of Packages, Using Packages, Creating Packages

An interface is a named collection of method definitions(without implementation).it also inclides


constant declarations.An interface is basically a kind of class.It also contains methods and variables but
the method is abstract and the variable is final.In interface, the method does not include any code to
implement and data fields only contain constants.

The general syntax of interface is

interface InterfaceName

variable declaration;

method declaration;

Interface is similar to class which is collection of public static final variables (constants) and abstract
methods.

interface item

static final int code=100;

static final String name="Aayush";

void display();

Why we use Interface ?

It is used to achieve fully abstraction.

By using Interface, you can achieve multiple inheritance in java.

It can be used to achieve loose coupling.


properties of Interface

It is implicitly abstract. So we no need to use the abstract keyword when declaring an interface.

Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

Methods in an interface are implicitly public.

All the data members of interface are implicitly public static final.

Extending interfaces

Interface name2 extends name1

Body of name 2

For example, we can put all constants in one interface and all methods in other interface.

Interface ItemConstants

int code = 100;

String name = “Fan”;

interface Item extends ItemConstants

void display();

we can able to combine several interfaces into one.

Interface ItemConstants
{

int code = 1001;

String name = “Fan”;

interface ItemMethods

void display();

interface Item extends ItemConstants, ItemMethods

………………………………

………………………………

Implementing Interfaces

Interfaces are used as “superclass” whose properties are inherited by classes. It is therefore necessary to
create a class that inherits the given interface.

class ClassName implements Interfacename

body of className.

Here class className “implements” the interface interfacename.

Class classname extends superclass implements interface1,interface2,…


{

Body of classname;

Implementing multiple inheritance in java.

class student void putmarks()

{ {

int rollno; System.out.println("marks obtained");

void getnumber(int n) System.out.println("Marks1="+m1);

{ System.out.println("Marks2="+m2);

rollno=n; }

} }

void putnumber() interface sports

{ {

System.out.println("Roll number="+rollno); float score=6.7;

} void putscore();

} }

class test extends student class results extends test implements sports

{ {

float m1,m2; float total;

void getmarks(float mark1,float mark2) public void putscore()

{ {

System.out.println("sport score="+score);

m1=mark1; }

m2=mark2; void didplay()

} {
total=m1+m2+score; r.getmarks(77.5,88.8);

putnumber(); r.display();

putmarks(); }

putscore(); }

System.out.println("total score="+total); Output:

} Rollno=103

} Marks obtained:

class multpletest Mark1=77.5

{ Mark2=88.5

results r=new results(); Score=6.7

r.getnumber(103); Total=171.0

Differentiate between abstract class and interface

Abstract class interface

1. It is collection of abstract method and concrete


1. It is collection of abstract method.
methods.

2. There properties can be reused commonly in a 2. There properties commonly usable in any

specific application. application of java environment.

3. It does not support multiple inheritance. 3. It support multiple inheritance.

4. Abstract class is preceded by abstract keyword. 4. It is preceded by Interface keyword.

Which may contain either variable or constants. 5. Which should contains only constants.

6. The default access specifier of abstract class 6. There default access specifier of interface

methods are default. method are public.


7. These class properties can be reused in other 7. These properties can be reused in any other

class using extend keyword. class using implements keyword.

8. Inside abstract class we can take constructor. 8. Inside interface we can not take any constructor.

Example of Interface
// Interface.java
interface PI{
static final float pi = 3.1416f;
}
interface Area extends PI{
public float getArea(float x, float y);
}
interface Shape extends Area{
public void display(float x, float y);
}
class Rectangle implements Shape{
public float getArea(float x, float y) {
return x * y;
}
public void display(float x, float y){}
}
class Circle implements Shape{
public float getArea(float x, float y) {
return pi * x * x;
}
public void display(float x, float y){}
}
class Interface {
public static void main(String[] args) {
Rectangle r = new Rectangle();
Circle c = new Circle();
Shape s;
s = r;
System.out.println(s.getArea(2,3));
s = c;
System.out.println(s.getArea(4,0));
}
}

You might also like