aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange overriding" Watch "Strange overriding" New topic
Author

Strange overriding

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 10, 2000
Posts: 124
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

Joined: Jun 26, 2000
Posts: 29
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

Joined: Jun 28, 2000
Posts: 477
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

Joined: Jul 13, 2000
Posts: 38
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

Joined: Jul 13, 2000
Posts: 38
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange overriding