In: Computer Science
JAVA Questions
Answer both questions explaining what each is and using an example to back up your explanation.
1. What does it mean to pass a parameter when the formal parameter is some type of class?
2. What does it mean to pass a parameter when formal parameter is some type of interface?
Interface put the Constraint on classes which Implement it on defining the each function in the class which are declared in the Interface. In other word we can say that Interface is a type which a class implement. so every class which implement an interface can be assigned to a variable of Interface type.
Ans1: lets understand it by an example
/**
* This program illsurate the Passing an object as formal argument
in function
* findCircleArea and findSquareArea in ShapeDimensionClass class
which return the area of circle and are of squar respectivly
* */
//circle class
class Circle{
double r;
double pi;
public:
Circle(double r)
{
this.r=r;
pi=3.14;
}
double area()
{
return pi*r^2;
}
}
//square class
class square{
double side;
double pi;
public:
Square(double side)
{
this.side=side;
pi=3.14;
}
double area()
{
return side*side;
}
}
/**
* since findCircleArea and findSquareArea parameters are class
thats why we need to function since two function required to pass
two class Circle and Square
* */
class ShapeDimensionClass
{
public static void main(String arg[])
{
ShapeDimensionClass d=new ShapeDimensionClass();
Circle circle=new Circle(3);
Square square=new Square(3);
System.out.println(d.findCircleArea(circle));
System.out.println(d.findSquareArea(Square));
}
double findCircleArea(Circle circle)
{
return circle.area();
}
double findSquareArea(Square square)
{
return square.area();
}
}
When we use class as an formal argument in function parameter we have to make different function for different class. but here in Circle and Square class you will see that both the Class hava same function name area() but its functionality differ in both case so we can make our code mode resuable by using an interface.
Ans2. so when usingn interface as an formal argument in a function we can pass any class which implement this interface into the method. lets understand it by example below in which there is an Shape interface which implemented by both of out class Circle and Square . and we will have now a findShapeArea method whoes formal arguement if of Shape interface. now this function alone can be used to return the area of a circle and square as in code below.
/**
* This program illsurate the Passing an interface as formal
argument in function
* findShapeArea in ShapeDimensionClass class which return the area
of circle and are of squar both .
* */
//circle class
interface Shape{
double area();
}
class Circle implements Shape{
double r;
double pi;
public:
Circle(double r)
{
this.r=r;
pi=3.14;
}
double area()
{
return pi*r^2;
}
}
//square class
class square implements Shape{
double side;
double pi;
public:
Square(double side)
{
this.side=side;
pi=3.14;
}
double area()
{
return side*side;
}
}
/**
* since findShapeArea parameters are interface of Shape type thats
why we need only one function since since it can accept argument of
any class which implement Shape class
* */
class ShapeDimensionClass
{
public static void main(String arg[])
{
ShapeDimensionClass d=new ShapeDimensionClass();
Circle circle=new Circle(3);
Square square=new Square(3);
System.out.println(d.findShapeArea(circle));
System.out.println(d.findShapeArea(Square));
}
double findShapeArea(Shape shape)
{
return shape.area();
}
}
When function Parameter is of interface type it can accept class of any type which implement this interface. which mean why using interface as an formal parameter in our class we can make our method generic. which can accept any class of its type.
Note: inside the function whoes formal argument is an interface when we pass an object into it. we can call only thoes method of class which is declared in the interface. if we need to call the method of class which are not declared in interaface we need to externally cast the object into class of its type inside the function