Page 1 of 1

About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Oct 19, 2012 12:50 pm
by ETS User
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

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Oct 19, 2012 2:12 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Mon Feb 25, 2013 6:40 pm
by satar
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:

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();
	}
}
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Mon Mar 04, 2013 1:48 pm
by satar
Also a minor nit on this is to remove the extra "will" in the question itself.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Feb 27, 2015 11:14 am
by dmcinnis1
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Sat Jun 13, 2015 2:10 pm
by ElizabethCM
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!

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Sat Jun 13, 2015 8:21 pm
by admin
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!
You may not. Where is the code calling abstract method?

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Jul 03, 2015 9:27 am
by Sergiy Romankov
But method() calculate is abstract, and
x.calculate() actually calls abstract method.
I thought that compilator shouldn`t pass it through

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Jul 03, 2015 10:21 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Sun Jan 28, 2018 2:34 pm
by ArpRokz
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Sun Jan 28, 2018 10:12 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Mon Jan 29, 2018 1:50 am
by ArpRokz
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?

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Mon Jan 29, 2018 2:44 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Fri Feb 07, 2020 7:20 pm
by goscha01
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!

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Sat Feb 08, 2020 12:42 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Tue Feb 11, 2020 8:16 pm
by goscha01
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!

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Tue Feb 11, 2020 11:59 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Thu Feb 13, 2020 11:23 am
by goscha01
Oh, I got it! The issue is not the method implementation but the null reference. Is it right?

Re: About Question enthuware.ocajp.i.v7.2.967 :

Posted: Thu Feb 13, 2020 11:33 am
by admin
That's correct.