Ryan Anderson

Greenhorn
+ Follow
since May 18, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ryan Anderson

Does anyone know why the Float wrapper class constructor allows you to send in a double as a param to the constructor ?

I figured it would have at least thrown a runtime error but it runs fine.

But...the valueOf() method in the wrapper class will throw compile error if you try to pass in a double primitive but will let you pass in a double in string format

1 and 2 are true

But the return value must be either

a) same exact thing for primitives
b) subclass of the return type if its an object
c) implementing class of the return type if its an interface

Note: if you change the param types --> automatically an overload

This wont compile right away --> notice the Honda subclass where it tries to return a byte instead of int

So after reading this post and another one about static inheritance...I wanted to see the differences between instance inheritance and static inheritance.

I modified the example above to include a static method that is inherited by Daughter. Notice that Mother.foobar() is called in its constructor.

I don't understand why the static method is not inherited. If the same rules apply to hiding and inheriting then why not just say that the static method is inherited.

I know that it says in K&B that "a static method cannot be overridden" but if these same rules apply then aren't we overriding the method in the subclass when following the rules?
here's some more examples

straight out of eclipse

nice score!

How many months did it take you to study for exam ?
11 years ago
ex) small into big

int a = 100;
long b = a; //implicit cast, int always fits into a long

ex) big into small [illegal]

long m = 100l;
int x = m; //COMPILER ERROR

ex) big into small [legal]

long m = 100L;
int x = (int)m;

ex) float to int [illegal]

float y = 29.9f;
int x = y;

ex) float to int [legal]

float y = 29.9f;
int x = (int)y;

ex) float literal to int [illegal]

int x = 22.4449;

ex) float literal to int [legal]

int y = (int)22.4499;
So in the second case:

Animal a = new Horse();
Horse h = (Horse)a; //works fine

1) "Animal a" is a Reference variable to an Animal object OR any of Animal's subclasses (Horse,Frog,Dog,Cat,etc..)
2) Any object you assign to the Animal reference MUST implement all the methods declared in Animal.....you do not want to do something like a.method() and method is not defined
3) You create "new Horse()" object and assign it to the reference
4) "Horse h" is a Reference variable to a Horse object OR any of Horse's subclasses object (Stallion,other types of horses) --> Not its superclasses --> make a special note that a Horse reference cannot refer to a superclass object (ie. Animal object)
5) The compiler will always let you assign a superclass reference (Animal) to Horse WITH an explicit cast --> compiler does not look at the actual object that "a" is referring to --> by casting you are telling the compiler that the object should implement all the methods in Horse --> but it does not check you
6) At runtime --> when you try to do "Horse h = (Horse)a" --> the JVM goes and looks at the object that "a" is pointing to and sees that it is a Horse
7) It is happy because you are basically doing: Horse h = new Horse() --> everything is aok


Please add anything I left out or you think you be helpful.
Yes this confused me at first also and I'm still slightly confused.

So in the first case:

Animal a = new Animal();
Horse h = (Horse)a; //COMPILES --> BUT RUNTIME CLASSCASTEXCEPTION

1) "Animal a" is a Reference variable to an Animal object OR any of Animal's subclasses (Frog,Dog,Cat,etc..)
2) Any object you assign to the Animal reference MUST implement all the methods declared in Animal.....you do not want to do something like a.method() and method is not defined
3) You create "new Animal()" object and assign it to the reference
4) "Horse h" is a Reference variable to a Horse object OR any of Horse's subclasses (Stallion,other types of horses) --> Not its superclasses --> make a special note that a Horse reference cannot refer to a superclass object (ie. Animal object)
5) The compiler will always let you assign a superclass reference (Animal) to Horse WITH an explicit cast --> compiler does not look at the actual object that "a" is referring to --> by casting you are telling the compiler that the object should implement all the methods in Horse --> but it does not check you
6) At runtime --> when you try to do "Horse h = (Horse)a" --> the JVM goes and looks at the object that "a" is pointing to and sees that it is an Animal --> not a Horse
7) It throws an exception because you are basically trying to do the following: Horse h = new Animal() --> this is very illegal because you cannot assign a superclass object to a subclass reference
I decided this was really interesting so I tried to explore all the possible combinations of assignment involving superclass, subclass and interfaces.

Please comment/correct me or add more examples if you think they will add to the example.

sounds good. so we'll say that static methods are not inherited by subclasses but they are visible (with a permitting access mod).

You're right. Had a bad experience learning C so I have hard time praising it.
11 years ago
I don't know how static init blocks work.

But assuming they weren't there like below, then Sub.method() will indeed work. Just compiled and ran.

It's visible but not sure if inherited ??

C is Junk unless you're going to work on Hardware.

C++ is useful in very specific instances...you would probably need to be working for Oracle, Microsoft, Sun, SAP to justify it. The language syntax (pointers, memory mgmt) is very complex as compared to Java.

C# and Java are the industry leaders in most cases. Unless you need a lightning fast application, they work great. Need lightning fast then go to the C++ route.

My first Java book was "Big Java" by Cay Horstmann. Served me well. You can fetch a cheap past edition on half.com

Also look on google for java courses at major universities...they usually post all their slides/notes and most importantly --> PROJECTS!

It's very hard to learn java from a book. You need practical experience which is hard to find....but colleges/universities require all their java students to do projects and they will post the specs for these on their websites. Let me know if you need help finding.

here's a start: http://agile.csc.ncsu.edu/iTrust/wiki/doku.php

11 years ago