3

When I have these classes:

public class Master{

    public String test(){
        return "I am the master object";
    }

    public String boeh(){
        return "Only inside master";
    }

}

public class Slave extends Master{

    public String test(){
        return "I am the slave object";
    }

    public String mehh(){
        return "Only insde slave";
    }

}

I know I can do this: Master jedi = new Slave() (because Slave is a child type of Master).

And because I can... Why do I get "I am the slave object" while the variable is set to Master. And I get the result of Slave.test() but can't access Slave.mehh().

So whats the reason to give a variable a type when its ignored this why? Or in other words when Master jedi is actually Slave jedi what function does it has to declare Master?

6
  • This is an example of dynamic method binding, can't access Slave.mehh()- because, you can access only the methods that are overridden in the child class. Commented Jan 23, 2015 at 23:00
  • Are you asking about C# or Java? Commented Jan 23, 2015 at 23:01
  • This is the essence of (Java) polymorphism. If it didn't do this, then what would be the point of polymorphism? Commented Jan 23, 2015 at 23:02
  • @Vsevolod Goloviznin in case of general oop and in usage with java or c# Commented Jan 23, 2015 at 23:17
  • @sirwilliam Java and C# can handle subclassing differently. Commented Jan 23, 2015 at 23:21

2 Answers 2

4

This is called polymorphism (and it is, in fact, one of the main reasons why we use object-oriented programming). It allows you to call different methods (under the same signature) from a base type variable without knowing the type of the contained objects beforehand. So this allows you to have more abstracted code (code that doesn't closely depend on the exact implementations of its parts).

If you want the methods to be dispatched dynamically, based on their runtime type (their actual type), you use instance methods. If you want the methods to be statically dispatched based on their compile-time type (the type of the variable you have assigned them to), you can use static methods instead.

Sign up to request clarification or add additional context in comments.

2 Comments

So the purpose is something like having person object so its abstract but we can have a person which is actually a employee or a customer
@sirwilliam Yes. You just call the method as it's declared in the base class, and if the derived class offers a more specialized implementation of that method, then that implementation is called, without requiring you to change the code that calls it. This isn't obvious with local variables, but you'll see its uses in cases where you are receiving the object from other code (as an argument, for example) and you don't know the exact type.
2

Because of the way inheritence works. If a method is copied by the slave(the way you copied thetest method), the slave version of it always takes precedence. The reason you can't access the mehh is because it is not in Master, so the program can't find it. If you want to use all the methods of Master and Slave, do Slave s = new Slave();.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.