About Question enthuware.ocajp.i.v7.2.1329 :
Moderator: admin
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1329 :
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)
}
}
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)
}
}
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
You may want to check out http://www.xyzws.com/Javafaq/what-is-va ... adowing/15 and http://stackoverflow.com/questions/1207 ... en-concept
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Thu May 08, 2014 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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?
{
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
Which of the following method definitions will prevent overriding of that method?
// private methods are not inherited at all so there is no question of overriding a private method.
ok but what if is in the same class?
Code: Select all
private void m1()
ok but what if
Code: Select all
private void m1()
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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.
-
- Posts: 10
- Joined: Fri Nov 18, 2016 5:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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:
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");
}
}
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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.
-Paul.
PS. Please use code tags, that is, put code between
Code: Select all
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
I am very sorry, but:
word "abstract" means that there is no possibility to override this method, you just may only add a realization for it. Am i right?
Code: Select all
public abstract void m1()
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1329 :
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.Sergey wrote:I am very sorry, but:word "abstract" means that there is no possibility to override this method, you just may only add a realization for it. Am i right?Code: Select all
public abstract void m1()
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.
Who is online
Users browsing this forum: No registered users and 58 guests