Tuesday, November 15, 2011

What Is an Interface?

What Is an Interface?
In general, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks.
Within the Java programming language, an interface is a type, just as a class is a type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.

The bicycle class and its class hierarchy define what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn’t care what class of items it manages, as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.

To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on.

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following:
  • Capturing similarities among unrelated classes without artificially forcing a class relationship
  • Declaring methods that one or more classes are expected to implement
  • Revealing an object's programming interface without revealing its class
  • Modelling multiple inheritance, a feature that some object-oriented languages support that allows a class to have more than one superclass

Simple Program On Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle

/* Area Of Rectangle and Triangle using Interface * /

interface Area
{
float compute(float x, float y);
}

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

class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}

class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));

area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
/** OUTPUT **

Area Of Rectangle = 2.0
Area Of Triangle = 10.0
*/

Final Keyword and Example

/* All members and variables can be

overriden by default in subclasses.

Final Keyword Is Used in Java to

prevent the subclass from overridding

the members of the superclass. Making

a method final ensures that the

functionality defiened in the mathod

will never be altered in any way.

Similarily the value of the final

variable can never be altered in any

way. */
class fe
{
double fp;
int fr;

fe(double p1,int r1)
{
fp=p1;
fr=r1;
}
}

class se extends fe
{
double sp;
int sr;

se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp=p2;
sr=r2;
}
}

final class te extends se
{
double tp;
int tr;

te(double p1,int r1,double p2,int

r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp=p3;
tr=r3;
}

double average()
{
return (fp+sp+tp)/3;
}
}

class Student
{
public static void main(String args[])
{
te student = new

te(75.6,40,78.6,44,70.5,48);
System.out.println("Year\tRoll

no.\tPercentage");
System.out.println("FE\t"+student.fr+"

\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"

\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"

\t\t"+student.tp);
System.out.println("Average

performance is " + student.average());
}
}
/* Output *

Year Roll no. Percentage
FE 40 75.6
SE 44 78.6
TE 48 70.5
Average performance is

74.89999999999999 */

Multilevel Inheritance in java

Program that makes use of Multilevel Inheritance in java
/* program To Implement Multilevel Inheritance */
class Fe
{
double fp;
int fr;

Fe(double p1,int r1)
{
fp = p1;
fr = r1;
}
}

class Se extends Fe
{
double sp;
int sr;

Se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp = p2;
sr = r2;
}
}

class Te extends Se
{
double tp;
int tr;

Te(double p1,int r1,double p2,int r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp = p3;
tr = r3;
}

double average()
{
return (fp+sp+tp)/3;
}
}

class Student
{
public static void main(String args[])
{
Te student = new Te(78.5,41,71.73,44,79.8,48);
System.out.println("Year\tRoll no.\tPercentage");
System.out.println("FE\t"+student.fr+"\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"\t\t"+student.tp);
System.out.println("\nAverage Performance is " + student.average());
}
}

/* Output *

Year Roll no. Percentage
FE 41 78.5
SE 44 71.73
TE 48 79.8

Average Performance is 76.67666666666668 */

program to find percentage of student in 5 subjects

/* Java program to find percentage of student in 5 subjects

class Percentage
{
public static void main(String args[])
{
float Eng, Hindi, Marathi, Science, Social, percent;
Eng=Integer.parseInt(args[0]);
Hindi=Integer.parseInt(args[1]);
Marathi=Integer.parseInt(args[2]);
Science=Integer.parseInt(args[3]);
Social=Integer.parseInt(args[4]);
percent = (Eng+Hindi+Marathi+Science+Social)/5;
System.out.println("Subjects Offered : English, Hindi, Marathi, Science, Socail Studies");
System.out.println("Percentage"+percent);
}
}

Pyramid of numbers

class pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
}
}

/* OUTPUT

1
1 2
1 2 3

Find the Sum of digits of number

/* Simple Java Program To Find the Sum of digits of number */

class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digit :"+sum);
}
}

Arrays

/********** Implementation of Matrix Operation Using Arrays *********/
// Matrix Addtion, Subtraction, Transpose, Multiplication

import matrix;

class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
int mat2 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };

//Matrix A
System.out.println("\nMatrix A:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print("\t"+mat1[i][j]);
System.out.println("");
}

//Matrix B
System.out.println("\nMatrix B:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print("\t"+mat2[i][j]);
System.out.println("");
}
System.out.println("\nOperation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}

System.out.println("");
}

System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("");
}

System.out.println("3. Transpose Of A\n");
int trans[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
trans [i][j] = mat1[j][i];
System.out.print("\t"+ trans[i][j]);
}
System.out.println("");
}
System.out.println("4.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("");

}
}
}

/************* OUTPUT ***************



Matrix A:
1 2 3
4 5 6
7 8 9

Matrix B:
10 11 12
13 14 15
16 17 18

Operation ON Matrices
1.Addition

11 13 15
17 19 21
23 25 27
2.Subtraction

-9 -9 -9
-9 -9 -9
-9 -9 -9
3. Transpose Of A

1 4 7
2 5 8
3 6 9
4.Multiplication

84 90 96
201 216 231
318 342 366
*/
/******* Java Implementation of Matrix Operation Using Arrays *******/

class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };
int mat2 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
System.out.println("Operation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i <3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}
System.out.println("\t");
}

System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("\t");
}

System.out.println("3.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("\t");

}
}
}

/************* OUTPUT ***************

Operation ON Matrices
1.Addition

11 13 15
17 19 21
23 25 27
2.Subtraction

9 9 9
9 9 9
9 9 9
3.Multiplication

138 171 204
174 216 258
210 261 312 */

Employee Details and Salary

/* Simple Java Program For Displaying Emplayee Details : NAme, Id No. Designation, and depending on designation deciding the salary Of the Employee*/

class emp
{
public static int emp_no;
public static String name;
public static String designation;
public void showdata()
{
System.out.println("Employee number:--->"+emp_no);
System.out.println("Employee name:------>"+name);
System.out.println("Employee designation:--->"+designation);
}
}

class salary extends emp
{
public static float bsal,hr,da,tsal;
public void cal()
{
tsal=bsal+hr+da;
System.out.println("Total salary:---->"+tsal);
}
}

class empsal extends salary
{
public void calc()
{
if (bsal >=15000)
{
hr=0.3f*bsal;
da=0.2f*bsal;
}
else if (bsal >=10000)
{
hr=0.4f*bsal;
da=0.3f*bsal;
}
else
hr=0.3f*bsal+500;
da=0.2f*bsal+500;
}
public static void main(String args[])
{
empsal E=new empsal();
emp_no=Integer.parseInt(args[0]);
name=args[1];
designation=args[2];
bsal=Integer.parseInt(args[3]);
E.showdata();
System.out.println("Employee Salary");
E.calc();
E.cal();
}
}