Monday, 30 January 2017

Interfaces

Interfaces
Java doesn’t support multiple inheritances, a sub class cannot have more than one super class. But in many situations it may be needed to access variables and methods of more than one super class in a sub class. To solve this problem java provides an alternate method known as interfaces to support concept of multiple inheritance.
An interface is just like a class, it contains methods and variables but in different way. The Interface contains only abstract methods and final variables. Interface doesn’t contain code to implement methods and data fields, it contains contents.  The code of methods is defined into classes that implements interfaces.
Example:
interface RoomInterface
{
       static final int maximumvalue = 50;
       static final String roomType = "Bed Room";
       void area(int x, int y);
       void display();
}
Maximumvalue and roomType are static final variables, area() and display() are methods. The methods are not defined here only prototype of the methods is there in interface so semicolon is used at the end of method prototype.
Extending Interfaces
Extending interface is just like extending classes. Sub interface can inherit the methods and variables of super interface which can be achieved by using keyword extends.
Example:
interface RoomConstents
{
       int maximumvalue = 50;
       String roomType = "Bed Room";
}

interface RoomMethods
{
       void area(int x, int y);
       void display();
}

interface MyRoom extends RoomConstents, RoomMethods
{
       -------
-------
}
In this example we have defined a super interface RoomConstents which contains two variables. The value of variables defined in an interface remains constant wheather we use static final or not. It is not necessary to write static final in variable declaration, by default all variables of interface are static final. Another super interface is RoomMethods which contains only methods. Sub interface MyRoom inherits properties of both the super interfaces which is a special feature of interface. The sub interfaces cannot define methods which are already defined in super interfaces.
Implementation of interfaces
Interfaces are just like super classes, whose properties can be inherited in classes. To use interface we will need to inherit in a class.
Example:
interface RoomConstents
{
       ----------
}

interface RoomMethods
{
       ----------
}

class MyRoomClass implements RoomConstents, RoomMethods
{
       ----------
}
To inherit properties of an interface in a class “implements” is used, in this example MyRoomClass is a class which inherits properties two interfaces RoomConstents and RoomMethods using implements.

We can inherit a super class and interface together in a sub class.  
Example:
class Room
{
       --------------      
}

interface RoomConstents
{
       -------------
}

interface RoomMethods
{
       ------------------
      
}

class MyRoomClass extends Room implements RoomConstents, RoomMethods
{

       -----------------   
}
In this example we have inherited a super class named Room using extends and inherited two interfaces RoomConstents, RoomMethods using implements.
Example:
interface Area
{
       final static float pi = 3.14F;
       float getArea(float x, float y);
}

class Rectangle implements Area
{
       public float getArea(float x, float y)
       {
              return (x*y);
       }
}

class Circle implements Area
{
       public float getArea(float x, float y)
       {
              return (pi*x*x);
       }
}


public class InterfaceExample
{
       public static void main(String[] args)
       {
              Rectangle myRect = new Rectangle();
              Circle myCircle = new Circle();
             
              Area myArea; // Interface Object
              myArea = myRect; // Area refers to myRect object
              System.out.println("Area of Rectangle: " + myArea.getArea(10, 10));
             
              myArea = myCircle;
              System.out.println("Area of Circle: " + myArea.getArea(5, 5));
       }
}
The output of the program is:
                        Area of Rectangle: 100.0
Area of Circle: 78.5
Any number of classes can implement an interface.
Example 2: (Multiple inheritances)
class Student
{
       int rollNumber;
      
       void setRollNumber(int x)
       {
              rollNumber = x;
       }
       void showRollNumber()
       {
              System.out.println("Roll Number " + rollNumber);
       }
}

class Exams extends Student
{
       float exam1, exam2;
      
       void setMarks(float m1, float m2)
       {
              exam1 = m1;
              exam2 = m2;
       }
      
       void showMarks()
       {
              System.out.println("Marks Obtained");
              System.out.println("Exam1: " + exam1);
              System.out.println("Exam2: " +exam2);
       }
}

interface Sports
{
       float sportsMarks = 7.0F;
       void showSports();
}

class Results extends Exams implements Sports
{
       float totalMarks;
       public void showSports()
       {
              System.out.println("Sports Marks: " +sportsMarks);
       }
       void showResults()
       {
              totalMarks = exam1 + exam2 + sportsMarks;
              showRollNumber();
              showMarks();
              showSports();
              System.out.println("Total Marks: " +totalMarks);
       }
}

public class MultiInheritance
{
       public static void main(String[] args)
       {
              Results nextStudent = new Results();
              nextStudent.setRollNumber(454578);
              nextStudent.setMarks(76.5F, 66.5F);
              nextStudent.showResults();
       }
}
The output of this program is:
Roll Number 454578
Marks Obtained
Exam1: 76.5
Exam2: 66.5
Sports Marks: 7.0
Total Marks: 150.0




No comments: