Core Java - [Interface]

♠ Posted by Unknown in at 00:56

Java Interface

The Interface is a technique to implement the multiple Inheritance. Interface can be designed to only declare the prototype of methods and declare variables. In interface all declared variables to be constants values. and the methods are only declared with its prototype and its cannot be have its body into the interface.

The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method name, argument lists, and return types, but no method bodies. An interface can also contain variables, but these are implicitly static and final. An interface provides only a form, but no implementation.

To create, an interface , use the interface keyword instead of the class keyword. Like a class, you can add the public keyword before the interface keyword or leave it off to give “friendly” status so that is only useable within the same package.

To make a class that conforms to a particular interface use the implements keyword.

The class which can be use interface, is responsible to implement those methods which are declared in the interface.  All constants are also available to this class.

Once the Interface is created with its all method prototyping, it can use implements more than one classes by following syntax.

Syntax:

public interface Interface_Name
{
 constants_declaration;
 methods_prototyping;
};
class class_name implements Interface_Name
{
 [private/public] dataname;
 [private/public] methods; //class own
 [public] methods; //Implemented from Interface
};

Example:

/*All methods are by default abstract in interface,
means mustbe implemented by every class which are used this
interface.*/
package Vishal;
public interface Geometric
{
            public double PI = 3.14;
            public double area();
            public void get_radius(double x);
            public void get_xy(double x, double y);
};

import java.io.*;
import java.lang.*;
import Vishal.Geometric;
class  Rectangle implements Geometric
{
            private double height;
            private double width;

            public void get_radius(double x) //Abstract Method must be Implemented
            { }

            public void get_xy(double x, double y)
            { height = x; width = y; }

            public double area()
            { return(height*width); }
}

class ExampleInterface
{
            public static void main(String args[])
            {
                         Rectangle RObj = new Rectangle();
                         RObj.get_xy(5,6);
                         System.out.println("The Area Is : " + RObj.area());
            }
};

2 comments:

This is a list of 10 best practices that are more subtle than your average Josh Bloch Effective Java rule. While Josh Bloch’s list is very easy to learn and concerns everyday situations, this list here contains less common situations involving API / SPI design that may have a big effect nontheless.

java training in chennai

I love Clojure, and could talk about everything it does right, but it is an awful language to learn. I would recommend learning Racket. Racket was my first language. Racket is very similar to scheme, but it has dialects made specifically for new learners. It is very beginner friendly and much easier to jump into than Clojure.

java training in chennai

Post a Comment