Inheritance example – Magic Robots:
As an example of inheritance, let us consider broadening the world of robots to admit a different sort of Robot with additional special abilities. We can define a MagicRobot that is able to move around in the same way as an ordinary robot, but is also able to become invisible, so that it does not show up on the screen display. This property could clearly be useful if we are using the robots as part of a game. Perhaps less obviously, it could be used in a practical robotics application, to reduce clutter by allowing selective display on a grid containing many robots.
In Java we say that the subclass extends its superclass - for example, the start of the MagicRobot class definition looks like this:
public class MagicRobot extends Robot
{
...
This is shown diagrammatically in Figure, using UML notation.
Since a MagicRobot is just a special kind of Robot, you can invoke all the standard methods of the Robot class to move it around or to find out its position. To control the visibility of MagicRobot objects, we need some additional methods. For example, we can define methods called makeVisible and makeInvisible, which could be used as follows:
...
gandalf.makeVisible(); // only for magic robots
gandalf.moveTo(3, 3); // just like a Robot
...
gandalf.makeInvisible(); // only magic robots
gandalf.moveEast(); // just like a Robot
...
It is also useful to be able to check whether or not a magic robot is currently visible. For this we need another method, called isVisible, which returns a suitable value. What sort of value should be returned here? Clearly, there are two possible meanings to the return value - either the robot is visible or invisible. So perhaps we could return an integer where different numbers are used to indicate whether a robot is visible or invisible. This is a possible approach, but has some drawbacks. It is very common for a result or a variable to have only two possible values like this, so Java offers us a special data type known as boolean. Data like this has only two possible values, true or false.
This seems to be exactly what we need. So the method isVisible should be de?ned to return a boolean (that is, true or false) value as shown below:
public boolean isVisible ()
{
...
}
To make this work, the magic robot also needs some data to allow it to 'remember' whether or not it is visible. An instance variable that holds boolean data is just the thing.
This is defined as follows:
private boolean visible; // true or false
So the definition for the MagicRobot class has three additional methods and one additional instance variable, compared to the Robot superclass. Here is an almost complete definition:
public class MagicRobot extends Robot // incomplete
{
private boolean visible; // true or false
public void makeVisible ()
{
visible = true;
}
public void makeInvisible ()
{
visible = false;
}
public boolean isVisible ()
{
return visible;
}
}
Notice that we do not have to repeat any of the methods or the instance variables of the Robot class. The extends keyword ensures that, broadly speaking, these are available to MagicRobot objects automatically. There are some complexities about inheritance but to keep things simple here, we are deferring these to a later unit. There is one slight flaw in this definition. Can you spot it? It is related to the initial state of a MagicRobot. We do not yet have a constructor for MagicRobot objects - so when we create a MagicRobot object, it is not obvious whether it is visible or invisible. It seems safest to make them invisible at first (so they will not show up unless you want them to). We can add a constructor as follows:
public MagicRobot()
{
makeInvisible();
}
In this case, the constructor for MagicRobot will automatically invoke the constructor for its superclass, the Robot class, before it does anything else. This ensures that the initial position of a MagicRobot object is set when it is created, just as for ordinary Robot objects. Sometimes the situation for constructors is more complex than this, but the details of this will be explained in a later unit.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Inheritance example – magic robots homework and assignments? Live Inheritance example – magic robots experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Inheritance example – magic robots homework help, java assignment help and Inheritance example – magic robots projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours