Question

In: Computer Science

1. What does it mean to pass a parameter when the formal parameter is some type of class?

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?

Solutions

Expert Solution

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



Related Solutions

What does MFI mean? And what are your values for this parameter?
What does MFI mean? And what are your values for this parameter?
What does “exchange rate pass-through” mean? When the TLdepreciates, explain how it leads to an...
What does “exchange rate pass-through” mean? When the TL depreciates, explain how it leads to an increase in inflation and interest rates.
What does it mean when a class, method and varaible is declared final?
What does it mean when a class, method and varaible is declared final?
What is a “void” method? What does “pass by value” mean? What is the scope of...
What is a “void” method? What does “pass by value” mean? What is the scope of a variable? What does it mean to say that a method is overloaded?
this is a project management class 1. What type of cost does not depend on the...
this is a project management class 1. What type of cost does not depend on the size of a project? 2. What is expedited cost? Support your answer with at least three examples.
In C++ 1.Create a class Point which has a template parameter of the type of internal...
In C++ 1.Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the Point(2D, 3D etc.). Store a statically allocated, internal array of type T with dimension n. Ensure to include any constructer(s),destructors, getters or setters you might need. (10 points) 2.Create a template function which computes the Euclidean distance between 2 points. (6 points) 3.Instantiate two Point<double, 3> and compute their distance. Instantiate two Point<int,...
1. What does Tilly mean when he says that some economists criticize progressives for killing the...
1. What does Tilly mean when he says that some economists criticize progressives for killing the goose that lays the Golden egg ? Why do advocates of unfettered capitalism defend inequality ? Are they right? Would Raising taxes on rich people slow economic growth ? 2. Who are “essential workers” during the current pandemic? Since they are “essential,” are they paid high wages? Should they be?
What does it mean when a cost-capacity factor is less than 1? What does it mean...
What does it mean when a cost-capacity factor is less than 1? What does it mean when it is greater than 1?
What does “double dividend” mean when it is associated with emissions taxes? In the class, we...
What does “double dividend” mean when it is associated with emissions taxes? In the class, we discussed the Washington carbon tax initiative. What does this initiative do? (You do not need to write down the exact numbers associated with the initiative.) Who are the big winners and big losers of the initiative?
What does it mean when the market rates for some of the benchmark jobs do not...
What does it mean when the market rates for some of the benchmark jobs do not fall inside the proposed pay structure, or, in some cases, at the extreme points in the range?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT