Guide to Java by James T. Streib & Takako Soma
Author:James T. Streib & Takako Soma
Language: eng
Format: epub
Publisher: Springer London, London
Using this new class, a programmer could create three different instances of the PointD class as follows:
PointD p1, p2, p3;
p1 = new PointD();
p2 = new PointD(1.0,1.0);
p3 = new PointD(p2);
Notice that the objects are being created using three different constructors. The only difference is the number of arguments. Further, since the first constructor ensures that coordinates referenced by p1 will be initialized to 0.0, the second constructor initializes the variables referenced by p2 via the arguments, and the third constructor makes a copy of the previous object which will be referenced by p3, the set methods do not need to be called. However, if the values in the points need to be changed later, the set methods are still there if necessary.
If a constructor is not included in a class by the programmer, the system will generate a default constructor. Should the programmer include a constructor without any parameters, then this constructor overrides the default constructor generated by the system. Although a bit confusing, this constructor provided by the programmer is also sometimes called a default constructor since it overrides the system default constructor. However, if one writes the two new constructors above, and a default constructor is not included by the programmer, then the system will not generate a default constructor. In such a case, were one to code a p1=new PointD(); statement, a syntax error would occur. The result is if one wants to override the system default constructor, it is a good idea to override it with a programmer-defined default constructor to avoid a possible syntax error. Even if overloading is not being used in the class, it is generally best for a programmer to include a default constructor and not rely on the system default constructor.
Just as constructors can be overloaded, so can methods. As with constructors, the name of the method can be the same, but the number of parameters, the types of the parameters, or the order of the different types of parameters must be different. For example, take the distance method from Sect. 5.1 which requires one parameter as shown again below:
public double distance(PointD p) {
double dist;
dist=Math.sqrt(Math.pow(x-p.getX(),2)
+ Math.pow(y-p.getY(),2));
return dist;
}
What if another method was needed to determine the distance of a point from the origin? Certainly one could invoke the method above by having one of the two points as the origin using the new constructors introduced in this section as follows:
PointD p1, p2;
p1=new PointD();
p2=new PointD(3.0,4.0);
dist = p2.distance(p1);
In this example, the default constructor initializes the coordinates of p1 to 0.0, and the second constructor initializes the coordinates of p2 to 3.0 and 4.0. But the assumption could be that the distance will be calculated from the origin, and it would be convenient not to need it as a parameter in the distance method. Such a method would look as follows:
public double distance() {
double dist;
dist=Math.sqrt(Math.pow(x,2)+ Math.pow(y,2));
return dist;
}
Instead of invoking the previous method with the dist = p2.distance(p1); statement, it could be invoked using the new method as follows:
dist = p2.distance();
Again, the name of the method is the same, but the number of parameters is different.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12577)
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Dependency Injection in .NET by Mark Seemann(9340)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8301)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7697)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7092)
Microservices with Go by Alexander Shuiskov(6859)
Practical Design Patterns for Java Developers by Miroslav Wengner(6777)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6719)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6419)
Angular Projects - Third Edition by Aristeidis Bampakos(6130)
The Art of Crafting User Stories by The Art of Crafting User Stories(5654)
NetSuite for Consultants - Second Edition by Peter Ries(5587)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5395)
Kotlin in Action by Dmitry Jemerov(5066)
