Explain overriding methods and the solution, JAVA Programming

Explain Overriding Methods: The Solution

The object oriented solution to this problem is to describe a new class, call it SlowCar, that inherits from Car and imposes the additional constraint in which a car may not go faster than 70 mph (112.65 kph).

To do this you'll required to adjust the two places that speed can be changed, the constructor and the accelerate() method. The constructor has a different name since all constructors are named after their classes other than the accelerate() method must be overridden. This means the subclass has techniques with the similar signature as the method in the superclass.
public class SlowCar extends Car {

private static final double speedLimit = 112.65408; // kph == 70 mph

public SlowCar(String licensePlate, double speed, double maxSpeed,
String make, String model, int year, int numberOfPassengers, int numDoors) {

super(licensePlate, 0, maxSpeed, make, model, year,
numberOfPassengers, numDoors);
this.accelerate(speed);

}

public void accelerate(double deltaV) {

double speed = this.getSpeed() + deltaV;

if (speed > speedLimit) {
super.accelerate(speedLimit - this.getSpeed());
}
else {
super.accelerate(deltaV);
}
}
}
The first thing to remember about this class is what it doesn't have, getSpeed(), getLicensePlate(), getMaximumSpeed(), setLicensePlate() methods or speed, maxSpeed and numDoors fields. All of these are given through the superclass Car. Nothing about them has modified so they don't required to be repeated here.

Further look at the accelerate() method. This is various than the accelerate() method in Car. It imposes the additional constraint.

The constructor is a little more complicated. First remember that if you're going to use a non-default constructor, which is a constructor with arguments, you do required to write a constructor for the subclass, even if it's just going to do the exact similar thing as the matching constructor in the superclass. You cannot easily inherit Car's constructor because in which constructor is named Car() and this one must be named SlowCar().

The constructor requires to set the value of name, url, and description. Therefore they're not accessible from the subclass. In the other words they are set through calling the superclass's constructor using the keyword super. While super is used as a method in the first non-blank line of a constructor, it stands for the constructor of this class's superclass.
The immediate superclass's constructor will be known as in the first non-blank line of the subclass's constructor. If you don't call it explicitly, then Java will call it for you along with no arguments. It's a compile time error if the immediate superclass doesn't have a constructor with no arguments and you don't call a various constructor in the first line of the subclass's constructor.

The use of the ternary operator in the constructor call is unusual. Therefore, it's essential to meet the compiler's requirement that the invocation of super be the first line in the subclass constructor. Or else this could be written more clearly using only if-else.

Posted Date: 5/15/2013 6:31:19 AM | Location : United States







Related Discussions:- Explain overriding methods and the solution, Assignment Help, Ask Question on Explain overriding methods and the solution, Get Answer, Expert's Help, Explain overriding methods and the solution Discussions

Write discussion on Explain overriding methods and the solution
Your posts are moderated
Related Questions
Do you provides a Complete source Codes for this application: " a. Write an application for a video store. Place the names of 10 of your favorite movies in a combo box. Let the use

JMS ((java messaging service))is an acronym used for Java Messaging Service. It is Java's answer to formed software using asynchronous messaging. It is one of the official specific

Need Mahjong puzzle game I want someone to make this game ( Mahjong puzzle game) for me in Android ONLY. If you have high level programming knowledge in Android then please b

What is Invoking static methods If a method or field is declared static, you access it through using the class name rather than a reference to a particular example of the class

Explain BIOS in java language? BIOS basically stand for Basic input/output system it is also called as the system BIOS or ROM BIOS is a de facto standard describing a firmware

What can you do with an exception once you've caught it? 1. Fix the difficulty and try again. 2. Do anything else instead. 3. Exit the application along with System.exit() 4

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The disadvantage of this bean is that it cannot be reused any

I want cyclos mobile banking USSD and IVR module Project Description: We already have a Cyclos mobile banking project deployed on our server with SMS banking module. The s

Solve Neural Network Questions Project Description: You must know  Binary Chop and Kohonen Network, Backpropagation Algorithm. Implementation is required. Can using existi

how to retrieve data from Excel file, and display that data in table on jsp page?