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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

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

Post 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.

satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

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

Post by satar »

Also a minor nit on this is to remove the extra "will" in the question itself.

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post 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.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

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

Post by Sergiy Romankov »

But method() calculate is abstract, and
x.calculate() actually calls abstract method.
I thought that compilator shouldn`t pass it through

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

goscha01
Posts: 6
Joined: Fri Feb 07, 2020 7:16 pm
Contact:

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

Post by goscha01 »

Oh, I got it! The issue is not the method implementation but the null reference. Is it right?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That's correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests