Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Constructor within the Java forums, part of the Software Development category; How constructor is advantageful?...
|
|||||||
|
|||
|
Re: Constructor
hi friend..
Every class has at least one it's own constructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor. In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In the Construct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor. class another{ int x,y; another(int a, int b){ x = a; y = b; } another(){ } int area(){ int ar = x*y; return(ar); } } public class Construct{ public static void main(String[] args) { another b = new another(); b.x = 2; b.y = 3; System.out.println("Area of rectangle : " + b.area()); System.out.println("Value of y in another class : " + b.y); another a = new another(1,1); System.out.println("Area of rectangle : " + a.area()); System.out.println("Value of x in another class : " + a.x); } } Thanks Deepasree |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| constructor | emb.mallik | C and C++ | 1 | 03-02-2009 04:50 PM |
| Copy Constructor | manoj200724 | C and C++ | 7 | 06-15-2008 04:04 PM |
| Constructor and Servlet Config | kannappanr | Java | 0 | 02-26-2008 12:56 PM |
| Constructor and init() | pramod.kuber | Java | 3 | 11-04-2007 02:42 AM |
| why i am getting an error while calling a constructor from another constructor | raj_java_j2ee | Java | 4 | 07-22-2007 03:39 AM |