Overriding concept

Sachin Jain

Sachin Jain

@sachin-0wuUmc Oct 17, 2024
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 ?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • csiscool

    csiscool

    @csiscool-qA5mGA Dec 11, 2010

    ya we can 😀 But you need to assign the base class instance to child class instance.
  • Sachin Jain

    Sachin Jain

    @sachin-0wuUmc Dec 12, 2010

    @ csiscool

    Can you please explain more ?
  • csiscool

    csiscool

    @csiscool-qA5mGA Dec 12, 2010

    What is the real purpose of doing so?

    PS: google it. you can find it
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Dec 13, 2010

    Yes you can by overloading concept..
    Check this code...
    class k
    {
    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();
    }
    }
    Otherwise it will hide the method of base class.
  • csiscool

    csiscool

    @csiscool-qA5mGA Dec 13, 2010

    mohit007kumar00
    Yes you can by overloading concept..
    Check this code...

    Otherwise it will hide the method of base class.
    Hey he is asking in the case of overriding., but you have posted in overloading,why?
  • Deepika Bansal

    Deepika Bansal

    @deepika-jf1ysv Dec 13, 2010

    Yes 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..
  • Insatiable

    Insatiable

    @insatiable-S9ushb Dec 14, 2010

    Polymorphism 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 Jain

    Sachin Jain

    @sachin-0wuUmc Dec 15, 2010

    But 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 ?
  • Insatiable

    Insatiable

    @insatiable-S9ushb Dec 15, 2010

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