-6

Can someone please explain to me how to implement these these types of code and explan what the difference between the three are? I am coding in java.

Royboss
  • 1
  • 2
  • 4
    Have you tried looking into it or do you just want somebody to google that for you? – q99 Aug 08 '13 at 07:22
  • This site is about fixing problems and not help you google something, but because I'm a nice guy, here is the official [tutorial on inheritence](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html), that should answer your questions. – ssindelar Aug 08 '13 at 07:22
  • This question is too broad - if you want a tutorial, there are plenty on the internet. It's fine to come back here if you have a more specific question. – mikera Aug 08 '13 at 07:22
  • http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 both other 'types of code' you can find at the same place – ice Aug 08 '13 at 07:23

3 Answers3

1

I'm goign to take a shot at it because I recently tried to understand those and this is a good way to see if I did :) because If you can't explain something you haven't really understood it :)

Casting is fairly simple. It means to pretty much convert a value or object of a certain type to a different type. This way you can for example turn a float into an integer

float y = 7.0
int x = (int) y

x will now be 7. Of course you can't simply cast any type to any other type. There are limitations which you should search for on google - i could never cover all of them.

Polymorphism sounds similar but is actually something else. As I understand it it means that certain objects can be of multiple types. For example of you have a class that extends another class any instance of the parent class can also be of the type of the derived class.

class Base {...} 
class Derived extends Base {...} 

Base obj1 = new Base();
Derived obj2 = new Derived();

obj1 = obj2;

Over the course of this snippet obj1 will have been an instance of Base first but then it will be an instance of Derived which is a class derived from base. This is possible because instances of derived classes contain an "inner object" (i don't know the official name) of the base class. When you cast the Base instance to an instance of Derived you will actually get this "inner object"

Hope this helps

pypat
  • 1,096
  • 1
  • 9
  • 19
0

True polymorphism (i.e., multiple inheritance) is not available in Java. However, you can get a good approximation using "Interfaces", though your classes need to implement all of the functions provided by the interface (link to Java Interfaces).

You can also emulate multiple inheritance using delegated setters/getters on classes. This can be complicated, but it can also give you the effect of multiple inheritance.

This topic is discussed at length in this Stack Overflow post.

Community
  • 1
  • 1