Page 1 of 1

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

Posted: Sun Mar 09, 2014 4:32 am
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)
}
}

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

Posted: Sun Mar 09, 2014 5:31 am
by admin

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

Posted: Wed Jul 02, 2014 2:33 am
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?

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

Posted: Wed Jul 02, 2014 9:27 am
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.

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

Posted: Thu Feb 05, 2015 8:29 am
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?

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

Posted: Thu Feb 05, 2015 9:22 am
by admin
I don't understand what you mean. Can write some code to show what you mean?

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

Posted: Fri Nov 18, 2016 5:53 pm
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");
	}
}

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

Posted: Fri Nov 18, 2016 10:08 pm
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.

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

Posted: Thu Oct 19, 2017 1:09 pm
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?

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

Posted: Fri Oct 20, 2017 12:22 am
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.