aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes This abstract method surprised me--Kathy&Bert Book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "This abstract method surprised me--Kathy&Bert Book" Watch "This abstract method surprised me--Kathy&Bert Book" New topic
Author

This abstract method surprised me--Kathy&Bert Book

Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
Chapter 2--problem 3
1)abstract class A{
2) abstract short m1();
3) short m2() { return (short) 420;}
4) }
5)abstract class B extends A {
6) short m1() {return (short) 42;}
7) abstract short m2();
8) }
I added line 7 according to the answer provided by the book, it compiles!
Are there really two m2() methods in class B? One from superclass A, another is "Re-declared" on line 7?


go...
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
No, there is only one method m2 because the abstract one (in class B) overrides the concrete one (in class A) since they both have the same signature.
[ March 05, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
Originally posted by Valentin Crettaz:
No, there is only one method m2 because the abstract one (in class B) overrides the concrete one (in class A) since they both have the same signature.
[ March 05, 2003: Message edited by: Valentin Crettaz ]

Did you mean "override" a method in superclass but without real implementation? If so, how can its subclass overridden version be invoked?
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109

An abstract class cannot be instantiated. You will have to implement the abstract method m2() first.
My Study Notes


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Paulo Freitas
Greenhorn

Joined: Feb 26, 2003
Posts: 17
Or maybe through the class B itself making use of anonymous class.
Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
Karin and Paulo,
Thanks!
But, if class B’s abstract method can NOT be called on its own version, is that still an overriding?
If not, what is it called? Not an overloading for sure.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
An instance method that is not abstract can be overridden by an abstract method. JLS 8.4.3.1
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
But, if class B�s abstract method can NOT be called on its own version, is that still an overriding?
If not, what is it called? Not an overloading for sure.

Overloading occurs when two methods have different signatures (different parameters) but the same identifier. Since the keyword "abstract" is not part of the signature, both methods m2 in class A and B have the same signature, so this is a real case of overriding.
It is perfectly admissible to override a "concrete" method with an abstract one, that is, override a method with an implementation with one without implementation.
Did you mean "override" a method in superclass but without real implementation? If so, how can its subclass overridden version be invoked?
If you subclass B and provide a concrete implementation of method m2 with a call to super.m2() within it, the compiler will complain (JLS 15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?, third bullet).
The following code proves that:

Output:


The link given by Marlene: JLS 8.4.3.1 abstract Methods
[ March 05, 2003: Message edited by: Valentin Crettaz ]
Jimmy Chiu
Ranch Hand

Joined: Feb 08, 2003
Posts: 58
Originally posted by Valentin Crettaz:
But, if class B’s abstract method can NOT be called on its own version, is that still an overriding?
If not, what is it called? Not an overloading for sure.

Overloading occurs when two methods have different signatures (different parameters) but the same identifier. Since the keyword "abstract" is not part of the signature, both methods m2 in class A and B have the same signature, so this is a real case of overriding.
It is perfectly admissible to override a "concrete" method with an abstract one, that is, override a method with an implementation with one without implementation.
Did you mean "override" a method in superclass but without real implementation? If so, how can its subclass overridden version be invoked?
If you subclass B and provide a concrete implementation of method m2 with a call to super.m2() within it, the compiler will complain (JLS 15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?, third bullet).
The following code proves that:

Output:


The link given by Marlene: JLS 8.4.3.1 abstract Methods
[ March 05, 2003: Message edited by: Valentin Crettaz ]

Thank you guys so much!
Now everything is clear.
 
wood burning stoves
 
subject: This abstract method surprised me--Kathy&Bert Book