• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and polymorphism  RSS feed

 
Sriram Akula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package pakage1;
public class Superclass
{
void method1()
{
System.out.println("This is method 1 of Superclass");
}
}

package package1;
import package2.*;
public class Other{
public static void main(){
Superclass sc=new Subclass();
sc.method1();
}
}

package package2;
import package1.*;
public class Subclass extends Superclass{

}

In the above code method1 is not available to Subclass. And sc is instance of Subclass. Can anyoneexplain why method1 of Superclass is called?
 
vvv gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

method1 is not available to Subclass Becas Your are using

---void method1()
{
System.out.println("This is method 1 of Superclass");
}

Default acces modifier fir Method1() .

So method 1 is only available or I would say its Visiblity is with in the Package A only You could not acess this method outside the Package A and Nither it is inhered in Subclass of package B.
 
samir singha
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not find any wrong with this code. it just worked fine when I compiled and ran it. Sriram can you send me what error message you are getting?
the Superclass and Otherclass are in same package that is in package1. so the method1 can be called from main method. if Otherclass is in package2 then method can not be called because it has a default access modifier also some times known as package access modifier
 
vvv gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srram

We are deviationg.

This is actually Query ci think code is running fine.

But we need to answer and Sort out

Ideally If if we create object of subclass and put that in super class reference and call the method which is available in subclass as well as super class So jvm give prefernce to method in Subclass.

But here MEthod is not available in Subclass and Still its running fine by calling superclass method.???


 
Sriram Akula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for not asking question properly...


the code i gave runs perfectly.

My question is that method1 is not available for Subclass .and how does it give the out[put instead of runtime error
 
Sriram Akula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my query is same as what Mr. Gupta has guessed
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism uses runtime binding. You declare an object of a particular typeand instantiate it with a different typewhich must be the same type or its subclass. You can only invoke methods which exist in the superclass, but methods are called from the runtime type of the objectprints "method1 of subclass."

If there isn't an overridden method, then the superclass method is called. Remember a Subclass object "IS-A" Superclass object. Everything the superclass does for anybody else (accessible methods), the subclass does too. So if you haven't overridden the method you will get "from superclass" printed out. Note what you have been told about access modifiers, too. Because your method isn't accessible to the subclass, it cannot be overridden in that subclass.
 
wayne zhong
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upstairs reply has explained everything.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Wayne Zhong, and we are pleased to have succeeded explaining things
 
Sriram Akula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie...got it clear now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!