
syntax :
for abstract class
abstract class class_name{
//access data type variables;
//access return type method_name();
//access return type method_name(parameter list){
//body of method;
}
}
for interface
interface class class_name{
//access final static data type variables;
//access return type method_name();
}
The variables declared in interface are treated as constant, they are declared with the helo of final and static keyword.Interface is very much helpful in creating multiple inheritance type relationship between the classes. This multiple inheritance cannot be achieved without the help of interface, as java dose not support multiple inheritance.
//creating interface
public interface rahul{
int age = 21 ;
public void show();
}
//implementing interface
class mohan implements rahul{
public void show(){
System.out.println("Hii my name is rahul.")
}
public void show2(){
System.out.println("Hii my name is mohan.")
}
}
class friends{
public static void main(String args[]){
mohan a1 = new mohan();
a1.show();
a1.show2();
}
}
In the above program we have created an interface "rahul" in which we have declared one integer type variable , and a method show() without any body. Now this interface is implemented by the class mohan by the use of implements keyword.
Interface can be extended.
One interface can also inherit resources from other interface by the use of extends keyword.The syntax is same as inheriting a class.
interface <name1>{
//access return type variables;
//access return type method();
}
interface <name2> extends interface <name1>{
//access return type variables;
//access return type method();
}
//example of a extending an interface
interface apple{
public int area();
public void show();
}
interface mango extends apple{
public int volume();
public void show2();
}
class input implements mango{
public int length, width ,height,orange,grapes;
public input(int x, int y , int z ){
length = x ;
width = y ;
height = z ;
}
public int area(){
orange = length*width;
return orange;
}
public void show(){
System.out.println("Length = " + length +" width = " +width + " height = " +height);
System.out.println(" area = " + orange);
}
public int volume(){
grapes = length*width*height;
return grapes;
}
public void show2(){
System.out.println( " volume = " + grapes);
}
}
class demo{
public static void main(String args[]){
input a1 = new input(4,6,8);
a1.area();
a1.show();
a1.volume();
a1.show2();
}
}
No comments:
Post a Comment