Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding a concrete method by abstract method

 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javaranchers
The below quetion is from a Mock exam
Which of the following statements is true?

a. An abstract method can not be overridden by an abstract method.
b. An instance method that is not abstract can not be overridden by an abstract method.
c. An abstract method declaration can not include a throws clause.
d. The body of an abstract method is represented by a set of empty brackets.
e. None of the above

Answer Given is e.None of the above.
but i expected b.
And i tried with some code to test


the above shows error at line 1 saying,This metthod requires body instead of ;
Then the anwer given in mock exam is wrong.....am i right???

thanks
Shakunthala Divakar
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about option a?
>> a. An abstract method can not be overridden by an abstract method.

I thought that was true. Can someone please explain why not?
 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi javaranchers
I am sorry.The code i was trying was wrong.
i have missed abtract modifier for print method.
The statement
An instance method that is not abstract can not be overridden by an abstract method...is right
 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry again
The statement is An instance method that is not abstract can not be overridden by an abstract method...is NOT right.
Hi Neha Bhattad ,
i think even option a. is not true so the given anwer in mock exam is right.
Try the below code


Thanks
shakunthala Divakar
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shakunthala,

In your code:


The abstract method in Subclass is the same as the one in the superclass...is it really overridden?? To me it looks like the method is plain inherited.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

yes, it compiles. It even compiles with an annotation:


But is this really an override?


Bu.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bu

To me method "print" doesn't seem to be overridden as neither of them have an implementation. Hence there is no behavior in the base class "print" which can be overridden in the sub class. My doubt is, if this is not overriding then what it actually is ? Is it something like base and sub classes having same named private methods which is again not a case of overriding.
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Sun Java

>> An abstract class can override an abstract method by providing another abstract method declaration.

Consider the case of an abstract method in an interface being overriden by an implementing abstract class.



>>An instance method that is not abstract can be overridden by an abstract method.

 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention the second code:
>> results in a compile-time error because the invocation super.toString() refers to method toString in class Point, which is abstract and therefore cannot be invoked.
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bu,

So is that truly an override? Does using the @Override change anything?

Thanks
Neha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic