Chapter 10 - Identifying Errors Flashcards
- Where is the error in this code sequence?
C c1 = new C( );
Correction:
D d1 = new D();
Error:
◊ The error is with instantiating abstract class object. ◊ C class is abstract, So cannot instantiate. ◊ To correct this , instantiate D object.
- Where is the error in this code sequence?
D d1 = new D( ); d1.foo1( );
The correct statement is follows:
d1.foo2();
Error:
◊ The error is with calling different class method.
◊ foo1() method is not available in D class.
◊ To correct this , call foo2 method.
◊ The foo1 method of class C is private and is not inherited by D.
◊ a D object
reference cannot call foo1.
- Is there an error in this code sequence? Why or why not?
C c2; c2 = new D( );
The program doesn’t contain any errors.
Reason for no errors:
◊ C is abstract class so we can instantiate using through child class D. ◊ With this instance we can call abstract class C methods.
- Where is the error in this new class?
public class E extends D { public void foo4( ) { super.foo4( ); System.out.println( "Hello E foo4()" ); } }
The correct statement is follows:
super.foo3(); // Line 5
Error:
◊ In Parent class D the foo4() method declared as private. ◊ Private methods have scope within class. So cannot use from other class. ◊ To correct this , call super.foo3() method.
- Where is the error in this class?
public class J extends I { }
The correct statement is follows:
Public class J implements I { } // Line 1
Error:
◊ Interface cannot be extended.
◊ Interface must be implemented.
◊ To correct this, instead of extends keyword use implements keyword.
- Where is the error in this class?
public class K { public void foo( ); }
The foo method does not have a method body; it must be declared abstract. The K class must be declared abstract as well.
The correct statement is follows:
Public void foo() {} // Line 3