Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Casting and constructor! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting and constructor!" Watch "Casting and constructor!" New topic
Author

Casting and constructor!

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
What is casting and default constructor?
Thanks
ads
Apu Nahasapeemapetilon
Ranch Hand

Joined: Sep 06, 2000
Posts: 51
Casting is the term used to describe the act of changing datatypes. For example;
String number = "12345";
int real_int;
real_int = (int)number; << this is casting a String to an int
The default constructor is a method that is either defined by the developer as a constructor with no arguments or inserted by the compile for every java class (as a constructor with no args) that does not have a constructor defined.
public class apu ()
{
public apu (){ << default constructor
//implicit call to super();
//constructor stuff
}
}
If a constructor method is defined with arguments, the default
constructor is not added by the compiler and will need to be coded by the developer if it is to be used.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Apu,
Thanks a bunch for ur help.
Casting is only possible between Datatypes or it is possible between objects?
Also, default Constructor is nothing but initialization of objects. Am i right?
Please clear my confusion,
Thanks again,
take care
ADS
Originally posted by ADS:
Hi,
What is casting and default constructor?
Thanks
ads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Hi Apu,
Also, consturctor of Subclass always call Super() of superclass?
please confirm!
Thanks
ADS
Originally posted by ADS:
Hi,
What is casting and default constructor?
Thanks
ads

Thillai Sakthi
Ranch Hand

Joined: Jun 17, 2000
Posts: 102
Apart from data type casting, object castings are also possible.
For example,
String s="Hello";
Object ob=(Object)s; //Here i am type casting the string into an object of object class.(as Object is the superclass of all classes)
Regarding yr second doubt, from a subclass constructor u can always call the superclass constructor by invoking super();, the only restriction being super() has to be the first statement of your sub class constructor. Hope this clarifies your doubts.


Regards,
Shakthi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Hi M Kumaresan

Thanks a lot!
Take care
ADS
Originally posted by ADS:
Hi,
What is casting and default constructor?
Thanks
ads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

A constructor is used to initialise an object when it is created. It is possible to pass arguments to the constructor ( to initialise it as we require). When the constructor takes no arguments (for eg. MyClass() ) then it is called the default constructor.
When you dont define any constructor in your code, the compiler automatically inserts a default constructor. However, if you have defined a constructor with arguments, then you have to explictly define the default constructor.
Is the point clear?
Good Luck
Ambi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Ambi
Thank a lot!!!
I like to know when I declare constructor with argument,then do i need to define default constructor or not?
Thanks in advance
Regards
ADS
Originally posted by ADS:
Hi,
What is casting and default constructor?
Thanks
ads

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
You cannot cast a String to a primitive. You can cast a char to another primitive, but you cannot cast an object to a primitive. You cannot cast a primitive to an object.
{
int i=1;
double d=3.4;
String s="Hi";
i = (int)d; // OK
i = (int)s; // no, no!
}
You CAN cast one object to another object.
Regarding your doubt about default constructor: When you do not provide any explicit constructors for your class, then the "default" no arg constructor is provided for you by java.
If you provide ANY constructor, there is NO "default" constructor. If you want a constructor with args and a constructor without args, you must provide both constructors.
Marilyn

[This message has been edited by Marilyn deQueiroz (edited September 19, 2000).]
[This message has been edited by Marilyn deQueiroz (edited September 19, 2000).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Marilyn,
thanks a lot!!!
ADS
Originally posted by ADS:
Hi,
What is casting and default constructor?
Thanks
ads

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting and constructor!
 
Similar Threads
Wrapper Doubt
Regarding Serialization
wrappers
Tring to do a spinner and i am getting errors
type casting of constructor