à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

OOPS in JAVA 20


OOPS in JAVA 20

Interface in Java

An interface in java is a blueprint of a class. It has static constants and abstract methods only.

Java Interface also represents IS-A relationship.

Syntax

interface interface_name {

}

Example:

/*Actual Code*/
interface Moveable 
{ 
 int AVERAGE-SPEED=0;   //what you declare
 void move();
}

/*Compiler Code*/
interface Moveable 
{ 
 public static final int AVERAGE-SPEED=40;   //what compiler sees
 public abstract void move();
}

NOTE : Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.

Interface differs from a class in following ways:

1. You cannot instantiate an interface.

2. An interface does not contain any constructors.

3. All of the methods in an interface are abstract.

4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

5. An interface is not extended by a class; it is implemented by a class.

6. An interface can extend multiple interfaces.

NOTE : A class extends another class, an interface extends another interface but a class implements an interface.


Why we use Java interface??

1. It is used to achieve complete abstraction.

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

3. It can be used to achieve loose coupling.

 

Rules for using Interface:

1. Methods inside Interface must not be static, final, native or strictfp.

2. All variables declared inside interface are implicitly public static final variables(constants).

3. All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.

4. Interface can extend one or more other interface.

5. Interface cannot implement a class.

6. Interface can be nested inside another interface.

Example of Interface implementation :

interface Moveable 
{
 int AVG-SPEED = 60;
 void move();
}

class Vehicle implements Moveable 
{
 public void move()
 {
  System.out.println("Average speed is"+AVG-SPEED");
 }
 public static void main (String[] arg)
 {
  Vehicle vc = new Vehicle();
  vc.move();
 }
}
Output :
Average speed is 60.

Multiple inheritance in Java by interface: 

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

Example:

interface Printable{  
   void print();  
}  
  
interface Showable{  
   void show();  
}  
  
class A implements Printable,Showable{  
  
public void print(){
   System.out.println("Hello");
}  

public void show(){
   System.out.println("Java");
}  
  
public static void main(String args[]){  
 A a = new A();  
 a.print();  
 a.show();  
 }  
}  

Output:
Hello
Java

Interface inheritance: 

A class implements interface but one interface extends another interface.

Example: 

interface Printable{  
   void print();  
}  

interface Showable extends Printable{  
   void show();  
}  

class Testinterface2 implements Showable{  
  
  public void print(){
    System.out.println("Hello");
  }  

  public void show(){
   System.out.println("Java");
  }  
  
  public static void main(String args[]){  
    Testinterface2 obj = new Testinterface2();  
    obj.print();  
    obj.show();  
  }  
}  
Output:
Hello
Java

Nested Interface in Java: 

Note: An interface can have another interface i.e. known as nested interface.

Example:

interface printable{  
 void print();  
 interface MessagePrintable{  
   void msg();  
 }  
}

Difference between abstract class and interface?

Abstract class Interface
Abstract class can have abstract and non-abstract methods.Interface can have only abstract methods.
Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
Abstract class can have final, non-final, static and non-static variables.Interface has only static and final variables.
Abstract class can have static methods, main method and constructor.Interface can't have static methods, main method or constructor.
Abstract class can provide the implementation of interface.Interface can't provide the implementation of abstract class.
The abstract keyword is used to declare abstract class.The interface keyword is used to declare interface.
Example:
public abstract class Shape {
    public abstract void draw();
}
Example:
public interface Drawable{
    void draw();
}

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website