• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

K&B book Chapter 2 Self Test Question No.3

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following,
1. abstract class A {
2. abstract short m1() ;
3. short m2() { return (short) 420; }
4. }
5.
6. abstract class B extends A {
7. // missing code ?
8. short m1() { return (short) 42; }
9. }

which three of the following statements are true?(Choose three)

A. The code will compile with no changes.
B. Class B must either make an abstract declaration of method m2() or implement
method m2() to allow the code to compile.
C. It is legal, but not required, for class B to either make an abstract declaration of method
m2()or implement method m2() for the code to compile.
D. As long as line 8 exists, class A must declare method m1() in some way.
E. If line 6 were replaced with �class B extends A {� the code would compile.
F. If class A was not abstract and method m1() on line 2 was implemented, the
code would not compile.

The answer is A,C,E. I agree with A and E. But the option C is somewhat weird. It talks of method m2( ) whereas it should talk of m1 ( ) (as per my view).Also, m2( ) is already declared and defined in class in class A, how come it can be re-declared in class B (as option C says). Please explain

Thanks in advance�
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niranjan,
method m2() in class A has already been implemented. So, leave it. B implements m1() alone.
So, we can infer the followings:
[1] The code is perfect and no changes required further. (This is what option A says).
[2]Class B needs not to be abstract since, it implements m1() (This is what option C says).
[3] Once again: class B needs not to be abstract (This is what option E says).
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mani,
Option C also implies that m2() can be overridden to be abstract.isnt it?

So can a concrete method be overridden to be abstract?
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cheenu,
Since m2() already implemented, it can be (or) can not be implemented again.
But, if it is made abstract, then the calss itself should be made abstract.
 
Sheriff
Posts: 22040
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any method can be overridden unless final, and the overriding method can be abstract (tested this with toString for another question around here).
This does require class B to be abstract, but guess what: IT IS.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

c. for class B to either make an abstract declaration of method m2()or implement method m2() for the code to compile.



Hi friends,

I think there is no need to make abstract declaration as well as implementation.

Is it right.
help plz..

Thanks
Regards
Hari Krishna
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...


//implementation of m2

abstract class B extends A {
short m2() {
return (short) 3;
}
short m1() { return (short) 42; }
}

//m2 as abstract

abstract class B extends A {
abstract short m2();
short m1() { return (short) 42; }
}


//without m2()

abstract class B extends A {

short m1() { return (short) 42; }
}


All the above 3 cases compile....

So the code first posted in the thread is legal....

which essentially means

it is not required,

for class B to either make an abstract declaration of method
m2()or implement method m2() for the code to compile


Regards
 
Rob Spoor
Sheriff
Posts: 22040
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not required no. But it is perfectly legal. And that is exactly what C says.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic