File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why does it throws compilation error in the subclass? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why does it throws compilation error in the subclass?" Watch "why does it throws compilation error in the subclass?" New topic
Author

why does it throws compilation error in the subclass?

shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48


Thanks,
Shoeb
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
but that is method overriding, right?

it is being written in the subclass
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

A class can not have two methods which has same Signature[name and arguments].

Overloading rule: Signature must be different
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
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

Joined: Feb 09, 2010
Posts: 21
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

Joined: Mar 14, 2010
Posts: 48


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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48


Now I am confused, how would I call the method in the subclass to print 50?
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
mehtodGetInt != methodGetInt.


SCJP 1.5(97%) My Blog
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
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.


Thanks,
Alps
shoeb sayyed
Ranch Hand

Joined: Mar 14, 2010
Posts: 48
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;
 
 
subject: why does it throws compilation error in the subclass?
 
Similar Threads
Override method
Inheritance
overriding / overloading
overriding in java
why does this not compile?