• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ask a questtion

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you tell me what is the difference between statement1 and statement2?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mei l.h.
First, please check out our naming policy here at the ranch. Please change your display name.
As for you question, that one little question can takes pages of explaination, depending on how detailed you want it. Here is the short answer:
The first one 'parent p=new sun();' is declaring a variable named p that can be used to refernece any object that is of type parent, the new variable is then set to reference an object of type sun (this is ok because a sun is a parent.
The second line 'sun s=new sun();' is declaring a variable named s that can be used to refernece any object that is of type sun, the new variable is then set to reference an object of type sun.
Now your question can lead into a gazillion different directions from here, things like inheritence, polymorphism and many others others...
Is there a certain part of this that you aren't clear on? Let us know what had you confused and we'll take a swing at it.
 
mei l.h.
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, that is cleared.
I am confused at Polymophism, can you explain more about it,thanks!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the campfire story on it. That should help. If you have questions after that, post them and we can clear up any lingering questions.
 
mei l.h.
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


through line 4 in mainclass.java, manager_method in manager could be used by line 5 in mainclass.java, but why system tell me "can't resolve symbol"?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, lets look at the two lines you're having a problem with. The first one:
Manager m=new Employee(); // error 3
You're trying to have a Manager variable reference an Employee object. The problem here is that an Employee is not a Manager. The next line:
Employee e=new Manager(); // ok 4
This is ok because a Manager is an Employee.
In your last error line:
String s=e.manager_method();// error 5 : can't resolve symbol???
The problem here is that there is no method in the Employee class called manager_method. You can only call the manager_method on a manager object. Even though the underlying object referenced by e is a Manager the compiler looks for the method based on the type of the variable not the object.
What you could do (this is one of the grest things about polymorphism) is have the Manager class override the employee_method to do things specific to managers. Then you can call the method like this:
String s=e.employee_method();
And get the functionality for managers that you provided in the Manager class. Like I said, at compile time the compiler checks to make sure there is a method with that name and signature in the class that is designated by the reference variable. The beautifull thing is that at run time it will call the correct method according to the underlying object, in this case the Manager object.
This is cool because you could create a collection of Employee objects and then for each one invoke the correct methods without having to know exactly which subclass of Employee they really are.
hope that helps
 
mei l.h.
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is clear, thank you for your explaination.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mei l.h.

Welcome to Javaranch.

Initials are not a last name. Please change your displayed "last name" to comply with our naming policy. "firstName lastName"

Thank you for your co-operation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic