Skip to main content

PROGRAMMING IN JAVA || USE INTERFACES

 

 II BCA - CORE PAPER 3

PROGRAMMING IN JAVA

USE INTERFACES

Contents:

  • Java Interface
  • Need for Java interface
  • Declaring an Interface
  • Relationship between Interface & class
  • Declaring Interface Constants 


Java Interface

  • ·         The interface in Java is a mechanism to achieve abstraction that specify what a class must do without saying anything about how the class will do it.
  • ·         An interface can be defined as a container that stores the methods to be implemented in the code segment.
  • ·         An interface can have methods and variables just like the class but the methods declared in interface are by default abstract.
  • ·         The variables declared in an interface are public, static & final by default.

Need for Java interface

There are mainly three reasons to use interface.

1.       It is used to achieve abstraction.

2.      By interface, we can support the functionality of multiple inheritances.

3.      It can be used to achieve loose coupling.

Declaring an interface

·         An interface is declared by using the interface keyword.

·         It provides total abstraction, where all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default.

·         A class that implements an interface must implement all the methods declared in the interface.

Syntax:

interface <interface_name> {
                // declare constant fields
                // declare methods that abstract by default.
               }

 

Example:

interface MyInterface {

                        public void method1();

                        public void method2();

}

 

The relationship between classes and interfaces

·         A class extends another class, an interface extends another interface, but a class implements an interface.



·         Though a class and an Interface look a little similar, they are extremely different from each other.

·         Some of the primary differences between class & Interface are given below as follows.

 

Interface

Class

Keyword used: interface

Keyword used: class

Interfaces do not have a constructor

Class includes a constructor

Interface stores only the signature of a method

Class stores complete method definition

Interfaces do not need Access Specifiers

In Class, Access Specifiers are mandatory

Interfaces do not include Data Members

Class includes Data Members

Interfaces do not have Static Members

Class includes Static Members

 

 The Rules for declaring methods and variables defined in the interface are :

  • ·          All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract.
  • ·         All variables defined in an interface must be public, static, and final—in other words, interfaces can declare only constants, not instance variables.
  • ·         Interface methods must not be static.
  • ·          An interface can extend one or more other interfaces.
  • ·          An interface cannot extend anything but another interface.
  • ·          An interface cannot implement another interface or class.
  • ·         An interface must be declared with the keyword interface.
  • ·         Interface types can be used polymorphically. 

Example:

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

} 

} 

public class TestInterface1{ 

public static void main(String args[]){ 

Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable() 

d.draw(); 

}


Compile       : javac TestInterface1.java

Run               : java TestInterface1

Output           : Drawing circle

 

Declaring Interface Constants

  • ·         A Java interface can contain constants. By placing the constants right in the interface, any class that implements the interface has direct access to the constants, just as if the class had inherited them.
  • ·         Generally interface constants are defined in an interface & they don't have to be declared as public, static, or final.
  • ·         Interface methods are always public and abstract.
  • ·         Any variable defined in an interface must be a public constant.

Example:

interface Foo {

int BAR = 42;

void go();

}

class Zap implements Foo {

public void go() {

       BAR = 27; // Since, BAR is interface constant, value cannot be modified.

         }

}

 

  • ·         In this example code, the value of a constant cannot be changed, once the value has been assigned.
  • ·         The Interface constant is declared, when the assignment happens in the interface.
  • ·         So the implementing class can access it and use it, but as a read-only value. 

 

 

 


Popular posts from this blog

EVOLVING ROLE OF SOFTWARE

EVOLVING ROLE OF SOFTWARE Software is both a product and a vehicle for delivering a product.

OPERATING SYSTEM || UNIT III || Memory Management - Basic Concept of Memory - Address Binding - Logical & Physical Address Space

  OPERATING SYSTEM UNIT III Memory Management ·          Memory Management is the process of coordinating and controlling the memory in a computer, Blocks are assigning portions that are assigned to various running programs in order to optimize the overall performance of the system. Computer Memory ·          Computer memory can be defined as a collection of some data represented in the binary format. ·          On the basis of various functions, memory can be classified into various categories. We will discuss each one of them later in detail. ·          A computer device that is capable to store any information or data temporally or permanently is called storage device. Need for Memory Management in OS Memory management technique is needed for the following reasons: Allocate and de-allocate memor...

Operating System || Unit I || Process Management

  Operating System Unit I Process Management Introduction to Process: A program in execution is called a process. The execution of a process must progress in a sequential fashion. In order to complete its task, process needs the computer resources. A process can be defined as an entity which represents the basic unit of work to be implemented in the system. The operating system has to manage all the processes which require the same resource at the same time , in a convenient and efficient way. The Process Management Involves, Scheduling processes and threads on the CPUs. Creating and deleting both user and system processes. Suspending and resuming processes. Providing mechanisms for process synchronization. Providing mechanisms for process communication. Process Architecture A program becomes a process, when it is loaded into the memory. A process in memory can be divided in 4 sect...