This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange overriding

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
can you look at the following code and tell me why it is not compiling. Also if I change the code to the Code2, it will compile.
Can anybody explain this strange behaviour?
Code 1:
abstract class javadoc{
public javadoc(){}
public javadoc giveBirth(){};
}
class javaprog extends javadoc{
public javadoc giveBirth(){
return new javaprog();
}
}
Code 2:
abstract class javadoc{
public javadoc(){}
abstract javadoc giveBirth(); }
class javaprog extends javadoc{
public javadoc giveBirth(){
return new javaprog();
}
}
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shikhar:
Hi all,
can you look at the following code and tell me why it is not compiling. Also if I change the code to the Code2, it will compile.
Can anybody explain this strange behaviour?
}

Your Code 1 Does not compile because :
A compile-time error occurs if the body of the method can complete normally (�14.1).
In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to "drop off the end of its body."
Therefore it gives the following error.
javaprog.java:3: Return required at end of javadoc giveBirth().
public javadoc giveBirth(){};
Your Code 2 : compiles prefectly with jdk1.2.2
Shikar, Going by the questions you have posted over the past week, I think u need to read the Java Language Specification if u have not already done so !
 
Kai Li
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shikhar:
Hi all,
can you look at the following code and tell me why it is not compiling. Also if I change the code to the Code2, it will compile.
Can anybody explain this strange behaviour?
Code 1:
abstract class javadoc{
public javadoc(){}
public javadoc giveBirth(){};
}
class javaprog extends javadoc{
public javadoc giveBirth(){
return new javaprog();
}
}
Code 2:
abstract class javadoc{
public javadoc(){}
abstract javadoc giveBirth(); }
class javaprog extends javadoc{
public javadoc giveBirth(){
return new javaprog();
}
}

For code 1:
class javedoc is not abstract class, since it doesn't include abstract method. You already give the implementation to methods. when you define public javadoc giveBirth(){}; you mean this method will return javadoc so you must include return in the method body, as to the type,it could be javadoc,like this:
public javadoc giveBirth(){return new javadoc();}
For code 2:
it is the correct code to implement the abstract class.And it works well.
Kai
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kai Li:
class javedoc is not abstract class, since it doesn't include abstract method. You already give the implementation to methods...


Kai: That isn�t the point, you can declare an abstract class even if you have no abstract methods, but you can�t declare abstract methods within a non abstract class.
 
kevin jia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For last question the answer is clear. But I have another question: Why we place a constructor in abstract class, though we cann't produce an instance of an abstract class.
I need your help.
Thanks
 
kevin jia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I find the answer. Even though we don't provide an constructor for abstract class, it also has a default constructor, this gurrantee the subclass of abstract class can inherite the function of all the ancestor class, it provide a bridge. If this point is correct?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic