Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polymorphism / Inheritence & Variable Declarations 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Ok, my brain is totally frazzled over this, partly because the courses system has a bug in it, and I came to completely the wrong conclusions then trying to understand inheritence...

So i'm hoping someone here can clear things up for me.

If I had a class ClassA & a sub-class of ClassA, called ClassB.

I understand that you can perfom the following...

Code:
ClassA myclass = new ClassB();

However the object instance referenced by myclass, although initialised as a ClassB object it is actually only a ClassA object and only instance attributes and methods from ClassA will be permitted / accesible. (in the course IDE, you can run methods of ClassB after creating the object as above, which apparently is wrong.) Hence my total confusion over all this.

So is the only reason a declared variable allows this is so you can use Polymorphism in your code and that is how polymorphic methods are exacutable, by allowing a required variable type to be passed as an argument, that is declared as either the required class or any super-class declared variable.

Which also helps in the creation and use of abstract classes for method sharing (polymorphic methods).

Is that right?

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
Well, talking about primitive types, you're converting
but it's still called casting isn't it?
Code:
Double myDouble = 12.34

int myInt = (int)myDouble;

(int) is a cast isn't it?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
Does that include String objects?

Is that why you need Integer.parseInt?

I'm assuming you cannot do...
Code:
String myString = "12345";

int myInt = (int)myString;

Never in front of your IDE when you need it !!!

I'll have to try that when I get home.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
You cannot cast a String to anything except Object, its only parent.

I like this thread, is like an exam for a Java certification

Cheers,
Dian
 
Thanks Dian,

I tried it and got
Semantic error: Cannot cast java.lang.String to int

I like this thread, is like an exam for a Java certification

Glad you like it, if it helps someone with their Java certification, that's great, just what we like to see.

either way it's certainly helped me with my goals :)




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> PERL v1.5 beta (FusionCharts)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top