This site requires JavaScript, please enable it in your browser!
Greenfoot back
davemib123
davemib123 wrote ...

2017/1/5

definitions

davemib123 davemib123

2017/1/5

#
Hi, could someone help me with definitions for 'polymorphism' and 'encapsulation'. I've had a read by doing some searches on google but they are quite complicated. Any help in giving a more Greenfoot-related answer?
danpost danpost

2017/1/5

#
See polymorphism in the java tutorials. Generally, it comes down to how subclassing can alter the states and behavior of an object while keeping within the classification of a more basic type. The java tutorials say this:
Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
This means making fields 'private' and adding public getter/setter methods, mainly.
davemib123 davemib123

2017/1/6

#
So if I read it correctly, would this be considered encapsulation: http://www.greenfoot.org/doc/howto-1
danpost danpost

2017/1/6

#
davemib123 wrote...
So if I read it correctly, would this be considered encapsulation: http://www.greenfoot.org/doc/howto-1
The 'theCounter' field is set 'private' and a public getter method is added, so data encapsulation is used there.
davemib123 davemib123

2017/1/6

#
great, thanks for the reply!
davemib123 davemib123

2017/1/8

#
for polymorphisim, i know the act() method is part of it. How about something like this: abstract class has this:
    protected void moveLeft(int xSpeed)
    {
        setLocation(getX() - xSpeed, getY());
    }

    protected void moveLeft(double xSpeed)
    {
        setLocation(getX() - (int)xSpeed, getY());
    }
then depending on the object it could call
moveLeft(4);
or
moveLeft(4.5);
is that on the right lines?
danpost danpost

2017/1/8

#
No. Calling an already existing implementation is not changing it at all. Polymorphism is when a change in implementation is exacted. Maybe this in a subclass:
protected void moveLeft(int xSpeed)
{
    setLocation(getX()-xSpeed, getY()+1);
}
Here, if the actor moves left, he moves diagonally down to the left instead of directly left. Or, maybe this:
protected void moveLeft(int xSpeed)
{
}
Now, the actor cannot move left at all using the 'moveLeft(int)' method, thus changing the implementation.
danpost danpost

2017/1/8

#
Anything that you do in a subclass that makes an object unique or different from objects of the parent class is a part of polymorphism.
davemib123 davemib123

2017/1/8

#
so by those comments, is it then a case of: 1) subclass inherits method from superclass 2) same method signature is used in the subclass but the method body is different
danpost danpost

2017/1/8

#
davemib123 wrote...
so by those comments, is it then a case of: 1) subclass inherits method from superclass 2) same method signature is used in the subclass but the method body is different
Yes. But there is more to it than that. Any new methods (additional behavior) or fields (additional states) that are included also 'morph' the object, as well as any code in the constructor of the subclass (that does something that is not already done by the execution of the constructors of the superclasses).
davemib123 davemib123

2017/1/8

#
great. now i get it :)
You need to login to post a reply.