About Question enthuware.ocajp.i.v7.2.967 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.967 :
A main() method in an abstract class goes against everything I thought I knew about abstract classes. I know that abstract classes can contain concrete and abstract methods but I did not think they could run. I thought that these classes could not be instantiated. I guess since they have constructors they can.
mind = blown
mind = blown
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
That is not what it means. To run the main method, the JVM does NOT have to instantiate the class. Remember that main is a static method, so no instance is needed. Therefore, the JVM has no issue while executing the main method even if the class is abstract.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Tue Feb 12, 2013 9:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
This one got me too but I see now that a variable of the abstract class to include a call to one of its abstract methods is allowed as the actual instantiation of it could easily be of a subclass that implements the method and thus valid. For example:
And I guess because the main is a static method not requiring an instantiation, it works too. Very intriguing question to say the least on a couple of levels.
Code: Select all
abstract class Calculator {
abstract void calculate();
}
class Ti extends Calculator {
void calculate() {
System.out.println("Only BIG number");
}
}
class ConstructorTests {
public static void main(String args[]) {
System.out.println("calculating");
Calculator x = new Ti();
x.calculate();
}
}
-
- Posts: 10
- Joined: Tue Feb 12, 2013 9:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Also a minor nit on this is to remove the extra "will" in the question itself.
-
- Posts: 16
- Joined: Wed Feb 25, 2015 8:52 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
I got this one wrong because I noticed that the abstract method calculate had not been implemented, so it wouldn't compile. I can see that if a subclass implemented the method calculate() then it would result in a NullPointerException at run time.
-
- Posts: 29
- Joined: Sun Apr 05, 2015 11:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Hi Paul,
Can you please explain me how come I may call a method declared as abstract? Why doesn't the compile complain?
Thanks a lot!
Can you please explain me how come I may call a method declared as abstract? Why doesn't the compile complain?
Thanks a lot!
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
You may not. Where is the code calling abstract method?ElizabethCM wrote:Hi Paul,
Can you please explain me how come I may call a method declared as abstract? Why doesn't the compile complain?
Thanks a lot!
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
But method() calculate is abstract, and
x.calculate() actually calls abstract method.
I thought that compilator shouldn`t pass it through
x.calculate() actually calls abstract method.
I thought that compilator shouldn`t pass it through
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Remember that all calls to non-private instance methods in Java are "virtual". Meaning, it is not the compiler that decides the actual method that will be invoked at run time. It is the JVM that decides and it decides based on the type of the actual object to which the reference variable refers. This is a fundamental OO concept in Java also known as polymorphism. I would suggest you to read this from a book before attempting mock questions.
Now, it is not the abstract method that is invoked. Look at the class of the object to which the variable is referring to? Is the method abstract in that class?
Abstract method is not implemented. There is no code for it. Therefore, it cannot be invoked. It is the actual non-abstract method implemented by the concrete class that is invoked. See the message posted above by satar.
HTH,
Paul.
Now, it is not the abstract method that is invoked. Look at the class of the object to which the variable is referring to? Is the method abstract in that class?
Abstract method is not implemented. There is no code for it. Therefore, it cannot be invoked. It is the actual non-abstract method implemented by the concrete class that is invoked. See the message posted above by satar.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
I think there is a mistake in the question given. The abstract class Calculator is not public but contains the main() method because of which the code does not compile and therefore the first option should be correct. I chose the first option when answering the question and was surprised when my choice was wrong. So I compiled the code and verified. Please correct me if I am wrong or else make the correct changes to the question.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
The question and the given answer are correct. You are probably using an IDE. You need to use the command line to compile and run the code while preparing for the exam.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jan 28, 2018 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Yes I was using an IDE. I tried running the program via cmd and it ran fine. Thank you for the clarification. I still have one question though why do we need to add the public modifier before the class name containing the main() method when writing a code in an IDE but it is not necessary when running programs in cmd ?
Should I consider this as an error in the actual exam if a main method is not within a public class?
Should I consider this as an error in the actual exam if a main method is not within a public class?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
IDEs try to make you write good code. That is why sometimes they enforces things that are merely conventions. The Java compiler works according to the specification. So you should always rely on the errors generated by the compiler on the command line for the purpose of the exam.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Hi!
After reading many times this discussion I still don't understand why admin wrote:
"Abstract method is not implemented. There is no code for it. Therefore, it cannot be invoked. It is the actual non-abstract method implemented by the concrete class that is invoked."
1. Where is in the code a concrete class?
2. Where is in the code an actual non-abstract method?
Thank you!
After reading many times this discussion I still don't understand why admin wrote:
"Abstract method is not implemented. There is no code for it. Therefore, it cannot be invoked. It is the actual non-abstract method implemented by the concrete class that is invoked."
1. Where is in the code a concrete class?
2. Where is in the code an actual non-abstract method?
Thank you!
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Goscha, You are taking that statement out of context. It is talking about the general concept i.e. what happens when the code contains a call to an abstract method. Something like this:
Vehicle v = new Car();
v.drive();
Assuming that Vehicle is abstract (with an abstract method named drive) and Car is a concrete subclass of Vehicle, the method drive() will actually be invoked on the Car instance.
In the given question, there is no concrete class and there is no call of the abstract calculate method anywhere in the code.
Vehicle v = new Car();
v.drive();
Assuming that Vehicle is abstract (with an abstract method named drive) and Car is a concrete subclass of Vehicle, the method drive() will actually be invoked on the Car instance.
In the given question, there is no concrete class and there is no call of the abstract calculate method anywhere in the code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Dear admin,
I understand that there is no call to the abstract method, but there is a call to a concrete method, which doesn't exist.
In your example, you refer to a concrete subclass of an abstract class. However, in our case, we neither have a concrete class nor a concrete method. So, I assumed if there is no such method, the NoSuchMethodError should be thrown.
Where does my logic fail?
Thank you!
I understand that there is no call to the abstract method, but there is a call to a concrete method, which doesn't exist.
In your example, you refer to a concrete subclass of an abstract class. However, in our case, we neither have a concrete class nor a concrete method. So, I assumed if there is no such method, the NoSuchMethodError should be thrown.
Where does my logic fail?
Thank you!
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
But the reference using which it is trying to invoke the method is null. So, before the situation for NoSuchMethodError arises, there is a NullPointerException.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Feb 07, 2020 7:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
Oh, I got it! The issue is not the method implementation but the null reference. Is it right?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.967 :
That's correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 46 guests