consider G and H almost identical classes public class G { public int funny(int
ID: 3631670 • Letter: C
Question
consider G and H almost identical classes
public class G
{
public int funny(int x)
{
if(x>=0)
return x*x;
else
throw new RuntimeException();
}
}
public class H
{
public int funny(int x) throw Exception
{
if(x>=0)
return x*x;
else
throw new Exception();
}
}
if we make G a subclass of H,compiler compiles the classes without complaint.but if we make H a subclass of G then compiler generates an error when we compile the two classes .explain compiler behavior in terms of liskov substitution principle(LSP)
where LSP is
(It is acceptable to make a class B a subclass of class A or to make B an implementer of interface A only if, for every method in both A’s and Bs interfaces, B’s method accepts as input all the values that A’s method accepts (and possibly more) and does everything with those values that A’s method does (and possibly more).
Explanation / Answer
throw new RuntimeException(); has narrow exception handlimg capacity.It can report only those error( exception ) which occur during run time. But throw new Exception(); handl allots of different exception including RuntimeException. That means throw new Exception is supper set of throw new RuntimeExcepyion.So the compiler report error when we make H a subclass of G according to LSP.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.