CrazyEngineers
  • Overriding concept

    Sachin Jain

    Sachin Jain

    @sachin-0wuUmc
    Updated: Oct 17, 2024
    Views: 959
    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 ?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • csiscool

    MemberDec 11, 2010

    ya we can 😀 But you need to assign the base class instance to child class instance.
    Are you sure? This action cannot be undone.
    Cancel
  • Sachin Jain

    MemberDec 12, 2010

    @ csiscool

    Can you please explain more ?
    Are you sure? This action cannot be undone.
    Cancel
  • csiscool

    MemberDec 12, 2010

    What is the real purpose of doing so?

    PS: google it. you can find it
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • csiscool

    MemberDec 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?
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberDec 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..
    Are you sure? This action cannot be undone.
    Cancel
  • Insatiable

    MemberDec 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");
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Sachin Jain

    MemberDec 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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Insatiable

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register