Overriding concept
Suppose we have a base class A and a derived class B.
A and B both have same method as:-
void show() {
<-----
------>
}
Now in class B we also define show method with some other definition.
We say now show method is overridden.
But now i want to access the base class's show method using object of derived class.
Can we do that ?
A and B both have same method as:-
void show() {
<-----
------>
}
Now in class B we also define show method with some other definition.
We say now show method is overridden.
But now i want to access the base class's show method using object of derived class.
Can we do that ?
Replies
-
csiscoolya we can 😀 But you need to assign the base class instance to child class instance.
-
Sachin Jain@ csiscool
Can you please explain more ? -
csiscoolWhat is the real purpose of doing so?
PS: google it. you can find it -
Morningdot HabluYes you can by overloading concept..
Check this code...
class k
Otherwise it will hide the method of base class.
{
public void show(int x)
{
System.out.println("mohit kumar singh ");
}
}
public class p extends k
{
public void show()
{
System.out.println("mohit cboy hack the world");
}
public static void main(String args[])
{
k k1;
p p1=new p();
k1=p1;
p1.show(2);
p1.show();
}
} -
csiscool
Hey he is asking in the case of overriding., but you have posted in overloading,why?mohit007kumar00Yes you can by overloading concept..
Check this code...
Otherwise it will hide the method of base class. -
Deepika BansalYes you can do this using scope resolution operator, SRO ( :: ).
public class A //base class { public void show() { System.out.println("In base class"); } } public class B extends A //derived class { public void show() //over-ridden function { A::show(); //calling the function of base class System.out.println("In derived class"); } }
And not just this, you can use SRO to resolve other type of ambiguities also like between a global and a local variable with same variable name.. -
InsatiablePolymorphism With Function Overloading is a way out :-
class a{
void show(String x){System.out.println(x);}
}
class b extends a{
void show(String x, String y){}
}
class c {
public static void main(String []args){
a a1 = new b();
a1.show("base");
}
} -
Sachin JainBut i wanted over-ridden method of parent class to be called using object of derived class.
Please do not confuse with over-loading.
I hope i am clear now.
I have to call an over-ridden method of parent class using object of derived class.
Is that possible ? -
Insatiablethen there is no way out because if you want object of derived class with overriding it wont work because its the REFERENCE variables who decide which method to call while using function overriding and not the Objects.
You are reading an archived discussion.
Related Posts
If you desire to protect your USB from getting unwanted files like virus, worm, spy, trojan etc than you are at the exact place.
What I’m gonna tell you is...
Hi friends got an informative latest news for you all!There are many people out there complaining about the Firefox RAM Memory Bug. Its not a bug. It’s part of the...
hi friends,
i am 3rd year electrical and electronics engineering student
i am trying to do a project in this sem i had seen the list of projects here and...
Hi friends Can someone explain me why do the system files get corrupted often??
One of my system files got corrupted and i was not allowed to go inside.I formatted...
Hi friends one of my class friends, who had written the GRE test recently secured 1580/1600.I asked her for the reason of securing high score in GRE!She gave me some...