Page 273 - PowerPoint Presentation
P. 273
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
Difference between Constructor and Method in Java
There are many differences between constructors and methods. They are given below:
JAVA CONSTRUCTOR JAVA METHOD
A constructor is used to initialize the A method is used to expose the
state of an object. behavior of an object.
A constructor must not have a return A method must have a return type
type. unless if it is void.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a The method is not provided by the
default constructor if you don’t have compiler in any case.
any constructor in a class.
The constructor name must be the The method name may or may not
same as the class name. be same as class name.
QUIZ 5: Hardcoding: Write a program on a piece of paper that computes the area of Triangle,
Square, Rectangle and Circle. Each shape is the class name.
1. There will be 5 classes including the main class
2. Each class(shape) namely: Triangle, Square, Rectangle and Circle must perform a
computation to get the area of the said class(shape)
Formula to be use:
- To get the area of Triangle, use (0.5)*base*height
- To get the area of Square, use side*side
- To get the area of Rectangle, use length*width
- To get the area of Circle use, 3.14*radius*radius
49