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

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

Moderator: admin

Post Reply
shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post by shining_dragon »

what's the difference between overriding and shadowing in terms of methods?

Because as far as I know, the concept of shadowing when applied in variables is just the same with the concept of overriding methods; such that:

class A{
int x = 0;;
}

class B extends A{
int x = 1;

public static void main ( String... args ){
B b = new B();
int y = b.x >>>>here, the value would be 1 (from class B just like when methods of class B will be invoked when a method overrides from superclass)
}
}

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

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

Post by admin »

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

Chandni
Posts: 6
Joined: Thu May 08, 2014 12:13 pm
Contact:

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

Post by Chandni »

class Q
{
public static void m1()
{
System.out.println("Q");
}
}
class R extends Q
{
public static void m1()
{
System.out.println("R");
}
}

class RDemo
{
public static void main(String arg[]){
R r=new R();
r.m1();}
}


this code works completely fine. we have overriden pulic static void m1() method. SO how does your second option hold true? that it cannot be overriden?

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

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

Post by admin »

It depends on what is your definition on "fine". You code may compile but static methods are not overridden, they are hidden. Please go through the link posted above.
If you like our products and services, please help us by posting your review here.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

Which of the following method definitions will prevent overriding of that method?

Code: Select all

private void m1() 
// private methods are not inherited at all so there is no question of overriding a private method.

ok but what if

Code: Select all

private void m1()
is in the same class?

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

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

Post by admin »

I don't understand what you mean. Can write some code to show what you mean?
If you like our products and services, please help us by posting your review here.

alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

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

Post by alfredo.zuloaga »

The question is Which of the following method definitions will prevent overriding of that method?
but in the answer you put as valid

public static void m1()

but these is incorrect example:

Code: Select all

class TestClass extends test2{
	public static void m1(){
		System.out.println("12222");
	}
	
	public static void main(String[] args) {
		TestClass class1 = new TestClass();
		class1.m1();
	}

}

class test2{
	public static void m1(){
		System.out.println("lllll");
	}
}

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

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

Post by admin »

If you make a method static, that method cannot be overridden because the concept of overriding (and polymorphism) only applies to instance method. A static method can be hidden but cannot be overridden.
-Paul.

PS. Please use code tags, that is, put code between so that it is easier for others to read.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

I am very sorry, but:

Code: Select all

public abstract void m1()
word "abstract" means that there is no possibility to override this method, you just may only add a realization for it. Am i right?

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

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

Post by admin »

Sergey wrote:I am very sorry, but:

Code: Select all

public abstract void m1()
word "abstract" means that there is no possibility to override this method, you just may only add a realization for it. Am i right?
Yes, but it can be argued both ways. One can also say that the behavior of the method is changed from being abstract in the super class to being implemented in the subclass. So that way, it is being overriden.

Technically, whenever a subclass implements an instance methods that is also declared(and/or defined) in the superclass, it is called overriding.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests