• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does it throws compilation error in the subclass?

 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have two getNum() methods with no arguments in class Test.

Methods are identified by their name and the types of the arguments. You can't have two methods with the same name and the same arguments.
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but that is method overriding, right?

it is being written in the subclass
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class can not have two methods which has same Signature[name and arguments].

Overloading rule: Signature must be different
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but when over riding is done

we can have method name with the same signature

only the logic inside it can be changed , right?
 
Nehel Patel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method is said to be overridden when one is in parent class and another is in child class with the same name, same return type, same parameter.

But Here you are defining getNum() twice with same arguments(None Arguments) which should not be. Compiler will not know at run time which getNum() should be called. Methods can not be differentiated only on basis of return type.

So method should be defined only once with same name and same arguments. With same name but different arguments, you can define as many methods.
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why am i not able to access neither the variable nor the method in the Test Class?
I have made a an object 's' but i am not able to access "i". What is wrong here?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem isn't in accessing the field, the problem is you are trying to execute code directly in a class body. Code can only be executed in a constructor, initializer or method. Try putting that code into a main method instead.

Oh, and before you ask: you cannot override static methods or fields, only non-static methods. Static methods and fields will hide or shadow the equally named method / field from the super class. For these shadowing methods / fields the reference type is used, not the actual type. So in your example it will access the superClass version of i, not the subClass version.
 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now I am confused, how would I call the method in the subclass to print 50?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mehtodGetInt != methodGetInt.
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey dude just check out the method you are calling..

you are calling superClass's method
mehtodGetInt()
and not subClass's method
methodGetInt()
.

So its printing 10 and not 50....check it out.

 
shoeb sayyed
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it man.

When I call the methodGetInt(). The call goes to the superClass.

I have created an object using the subClass by saying [subClass s=new subClass()]

Now my question is I want to invoke the methodgetInt() of the subClass to print 10;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic