Jump to content

OOP Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
orang

orang

    Newbie

  • Members
  • Pip
  • 1 posts
I have a pretty basic question about what I think would be inheritance.
If you have a class Person and a class Boy that is a subclass of A and you do something like:

Person fred = new Boy();
Is the fred object a person or is it a boy?
And would method calls that are overridden in the Boy class be called or would the methods in the Person class be called? I have been trying to figure this out for a while, and I can't seem to get it. If anyone can clarify how this works that would be great. And I'm learning in java if that makes a difference.

#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
The static type of fred would be Person and the dynamic type would be boy. Method selection at runtime is based on the dynamic type. So I'd probably go with the methods in boy. I learning java too! ;)
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
fred is a Person and a Boy.

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If fred was not both a Boy and a Person, you would not be able to declare him as Person and then instantiate him as Boy.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
because boy is a sub class from person, this separation occurs when boy have diffrent variables and methods that arnt in person class and adding these variables and methods would effect the other types like "girl" or "mature"..etc.
in the same time theres certain attributes in person that will be used in the boy subclass
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript