Nitish Bangera wrote:public class Animal{public void methods(){}}
class Dog extends Animal{public void methods(){}}
Now in the main, if we write
Animal A = new Dog() //We can treat a Dog as an Animal
A.methods() // Will check in Animal in compile-time and then will run Dog method in runtime.
Dog D = new Dog() //Dog is treated as Dog only.
D.methods() // Will check and run Dog method in compile and runtime respectively.
but how will this object behave on its own?
new Dog().methods();
I need to know what will it check in compile time and what will it run in runtime.
[SCJP 6.0]
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Nitish Bangera wrote:Ok...Well i just got confused seeing the override of String... and String[]. Because if the Dog has a method(String[]) and Animal has method(String...) then how new Dog().method()(As it takes no arguments so String[] in Dog should not allow) behaves that i want to know. What it will check first? well the code compiles....seems it checks the Animal method and then runs dog method. I want to know is did the new Dog() check for method(String...) and then at the JVM it ran method(String[]) . Its clear when reference variables are there. But i am little confused when the objects are used for accessing methods.
Well the solution that i deduce from this is the Dog object has method(String...) takes in zero n more arguments and also method(Sting[]) which does not allow zero arguments. So when no arguments are passed, the method with String... parameter is the method which is matched and run. Is it correct???
[SCJP 6.0]
Nitish Bangera wrote:Now it makes perfect sense. So new Dog() used on its own is creating an anonymous sub class of dog which has the methods as it inherits from dog. Also the methods are searched by the compiler in the inheritance tree.
Thanks. Stephen.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Vinayagar Karpagam wrote:We always claim that if a class extends another class,
all the inherited methods of the parent class will be accessible on a child class object.
Vinayagar Karpagam wrote:But in this case, we have two methods which are not allowed to be kept as overloads in a class.
Then the sub-class method will override the parent class method.
This means that our sub-class Dog can have only one method, the overriding method (method(String[])).
Vinayagar Karpagam wrote:
And the compiler allows calls to both the overridden method and the overriding method. But at run-time,
it is always the overriding method that is being called.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Correct, the compiler only checks the left hand side of the assignment (the reference types) not the object types. Thus
in this case the Class types are checked, and sees that class Animal has a method called 'method'. Thus when it sees the Dog class
type with a reference to the same method, it allows it because a Dog 'is an animal' and an animal has the method, 'method'.
It then checks the signatures to make sure they are either valid overloads(as you can also have an overloaded method of a superclass within a subclass) or valid overrides.
The compiler will simply treat this as a new Dog object, it has no qualms about the type as there is no reference type (why its anonymous).
Thus the .methods will be a compiler check against the Dog object methods().
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Nitish Bangera wrote:
here.... new Dog().method() if it sees only dog class methods then Why does it compile? Here there is no reference hence no left hand side.
This code compiles properly n outputs Dog. Now i am asking, what did the compiler see to compile it without errors?
[SCJP 6.0]
Nitish Bangera wrote:
no reference variable then also the compiler will get to the method with parameter String... .
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
but it has no reference variables to check against for method invocations, so no it doesn't search the inheritance tree.
Nitish Bangera wrote
This code compiles properly n outputs Dog. Now i am asking, what did the compiler see to compile it without errors?
Nitish Bangera wrote:in the above program...
new Dog().method() compiles means the compiler is checking with the Animal's method(String... args) as it allows zero argument passing. Why?
if it had been Dog d = new Dog(); d.method(); then this is understood as the compiler looks in the inheritance tree for the method.
but it has no reference variables to check against for method invocations, so no it doesn't search the inheritance tree.
now if new Dog().method() runs means its getting a match with Animal's method(String... args) as dog's method(String[] args) doesn't allow zero arguments.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Vinayagar Karpagam wrote:
Are you sure the code compiles? As far as I believe the compiler wouldn't allow the call when reference type is not parent type.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Nitish Bangera wrote:no reference variable then also the compiler will get to the method with parameter String... .
[SCJP 6.0]
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Vinayagar Karpagam wrote:Yes, I tried. The following code doesn't compile.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
[SCJP 6.0]
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Anastasia Sirotenko wrote:See it like this:
when you call new Dog().method() it is just the same as you would do Dog d = new Dog();d.method();
But when you call new Animal() a = new Dog(); a.method() - here you just call compiler that only methods you are interested in your object are the methods it inhereted from Animal. And only those you will be able to call with a variable without cast to Dog.
just look at new Dog().method() as it was Dog d = new Dog();d.method();d = null;
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
just look at new Dog().method() as it was Dog d = new Dog();d.method();d = null;
So as there was no reference type declared, the compiler checks the class type and sees its of Type Dog, and uses that as an implicit reference to a Dog object, and thus checks to see if the reference shares the correct method signatures with Animal its superclass
Not so fast naughty spawn! I want you to know about
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|