• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This abstract method surprised me--Kathy&Bert Book

 
Jimmy Chiu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jimmy Chiu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An abstract class cannot be instantiated. You will have to implement the abstract method m2() first.
My Study Notes
 
Paulo Freitas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe through the class B itself making use of anonymous class.
 
Jimmy Chiu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance method that is not abstract can be overridden by an abstract method. JLS 8.4.3.1
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic