Here are some class definitions with questions that follow. interface MyInterfac
ID: 3531997 • Letter: H
Question
Here are some class definitions with questions that follow.
interface MyInterface
{
// Methods of MyInterface
}
public SomeClass implements MyInterface
{
// All methods of SomeClass and MyInterface
}
SomeClass obj = new SomeClass( );
a. MyInterface miobj = obj;
b. SomeClass obj2 = miobj;
c. miobj.aMethodOfSomeClass( );
A. Is statement a above legal, or is casting required? Explain.
B. Statement b is trying to assign the SomeClass object refered to by miobj to a SomeClass reference variable obj2. What is wrong with this statement and how would you fix it.
C. In statement c, an interface reference is trying to access a SomeClass class method that is not part of MyInterface. Is this legal? Explain.
Explanation / Answer
A) yes the first statement is legal, java will do the typecasting automitcally but throgh miobj we can call only those methods which are declared in MyInterface
B)obj2can call only those methods which are declared in MyInterface ,it cannot acces those methods which are define in class in SomeClass but not declare in the interface
C) it is illegal and the reason is explained above
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.