aspose file tools*
The moose likes Beginning Java and the fly likes quick class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick class question" Watch "quick class question" New topic
Author

quick class question

Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
"All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program."

When would it happen when the parent class has no constructor and the class is not associated with Object class?
Joseph Kampf
Greenhorn

Joined: Mar 04, 2004
Posts: 26
If the class you are extending doesn't have a default constructor. For example.

public class A extends Object{
public A(int i){}
}

public class B extends A{
}

Class B will not compile. Because a default constructor would be created that calls the default constructor of class A. But Class A does not have a default constructor, even though it extends Object.


Originally posted by Tony Smith:
"All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program."

When would it happen when the parent class has no constructor and the class is not associated with Object class?
Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
I think B is also an object, by definition all object has default constructor. Check out my following test code, it run just fine...

class A {
}

class B extends A {
}

public class test {
public static void main(String[] args) {

A a = new A();
B b = new B();
System.out.println("hello there");
}
}
Joseph Kampf
Greenhorn

Joined: Mar 04, 2004
Posts: 26
I'm not sure what your test code is trying to do.

However, B's parent is not java.lang.Object. B's parent class is A. Yes you can do something like:

Object b = new B();

And even:
A a = (A) b;

But you can do the same with interfaces, which don't have constructors. How you reference an instance has no bearing on it's constructor hierarchy.

Assume:

public class B extends A implement I{
}

You can do:

I i = new B();

Thanks,

Joe


Originally posted by Tony Smith:
I think B is also an object, by definition all object has default constructor. Check out my following test code, it run just fine...

}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tony Smith:
"... If the parent has no constructor (Object does have one), the compiler will reject the program." ...

I think this is trying to say, if the parent has no constructor that matches the subclass call (in terms of argument types), then a compiler error will result.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

Doesnt everything in Java extend the Object implicitly? (pure OO language).


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maneesh Godbole:
Doesnt everything in Java extend the Object implicitly? (pure OO language).

Yes, Java has a "singly rooted hierarchy," meaning that all classes are derived from a single class, Object.

However, this does not make it a "pure OO language" (which is a term that needs to be defined before using it to describe anything).
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

What I meant was, since every class extends the Object class, eventually the default "Object" constructor would be used.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maneesh Godbole:
What I meant was, since every class extends the Object class, eventually the default "Object" constructor would be used.

Absolutely right. Because all Java classes implicitly derive from Object, Object's constructor is called whenever anything is instantiated.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Originally posted by Tony Smith:
I think B is also an object, by definition all object has default constructor.


No. An object only has a default constructor if no explicit constructors have been defined.
So, this will have a default constructor.But this will not
And this class has an explicit no argument constructor.
Note that this is different to a default constructor, because it contains code. The default constructor would only contain a call to it's parent's no argument constructor.


Joanne
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10248
    
    8

Originally posted by marc weber:

However, this does not make it a "pure OO language" (which is a term that needs to be defined before using it to describe anything).


Oh!
I was under the impression that "pure OO" means everything is an object. I have read the typical comparisson between C++ and Java. You can write non OO code in C++ but cant in Java.

Ok there are primitives in Java but you also have corresponding Wrapper classes. So eventually you are dealing with object.(I am of the personal opinion that the Class class and the Object object are overkills!)

Doesnt this make Java a "pure OO language"?
What would be the proper definition of "pure OO" then?
rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
hi people,
1) a is class that extends Object class
2) b is a class that extends a

both don't have constructors so they call the parent constructor

so where does the complier give error???

this program just works fine......

class a extends Object{

}
class b extends a
{
/* b()
{
System.out.println("hello");
}*/


public static void main (String args[])
{
b b1= new b();
}

}


Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
The compiler will automagically add a default no argument constructor to both classes. The only thing these constructors will do is call their parent class's no argument constructor.
Because class a and b both have this default constructor and because the Object class has a no argument constructor, there will be no compilation errors.
However, try adding a constructor to class a that does have arguments and then compiling class b and see what happens. Post again if you don't understand the result.
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
"Default constructor" -> As it's clear by name "Default" means ,if you won't provide any specific value ,you will have some initial value automatically.

Basically , The basic purpose of "Constructor" is "to construct the object or to instatiate".

So ,no arg-constructor and args-construtor is the way to instantiate.
Regardsless of the mechanism you use, the basic purpose will be same.

As every body discussed about default costructor in object class and java is "single rooted hierarchy".

"Singly rooted hierarchy" it's serves a specific purposes like every object in java is child of "Object class".

Just consider for the example ,that what would happend if java has no default constructor?

Suppose ,if you have class

Class A{}
Class B extends A{}
Class C extends B{}

if you look at the above code ,you will find that there is no constructor and we know java is singly rooted hierarchy ,so how these class will communicate with each other.

The only way is either you provide some mechanism to communicate between objects or java should provide.

So ,it means you require and expect from java like something called "default functionality" where you dont want yourself to bother about communication between objects and instatiation.
That's y java has the functionality of default constructor.

If you not provide any constructor means you are using java default constructor way to communicate.
The time you provide any constructor in class ,you deprived of the default functionlaity.because you have defined you own way of constructing object.

Moral is ->
So, default constructor is one of the facility provided by java for devlopers not to bother the object
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Originally posted by Maneesh Godbole:


Oh!
I was under the impression that "pure OO" means everything is an object. I have read the typical comparisson between C++ and Java. You can write non OO code in C++ but cant in Java.


Erm, given your definition of pure OO, Java dosnt qualify, because it has primative data types, wrappers or not, they or still not a Class.

Also I am not quite sure I agree with "you cant write non OO code in Java." Yes you must have at least one class file with a main method to write an application in Java, but you could use static methods in class files write your code. It would be nasty looking code, but it would be possible, and it wouldnt be OO, of course using the library/package would introduce OO-ness but your code could still break the rules of OO.

Just my opionon of course.
G
[ August 22, 2007: Message edited by: Gavin Tranter ]
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
"Default constructor" -> As it's clear by name "Default" means ,if you won't provide any specific value ,you will have some initial value automatically.

Basically , The basic purpose of "Constructor" is "to construct the object or to instatiate".

So ,no arg-constructor and args-construtor is the way to instantiate.
Regardsless of the mechanism you use, the basic purpose will be same.

As every body discussed about default costructor in object class and java is "single rooted hierarchy".

"Singly rooted hierarchy" it's serves a specific purposes like every object in java is child of "Object class".

Just consider for the example ,that what would happend if java has no default constructor?

Suppose ,if you have class

Class A{}
Class B extends A{}
Class C extends B{}

if you look at the above code ,you will find that there is no constructor and we know java is singly rooted hierarchy ,so how these class will communicate with each other.

The only way is either you provide some mechanism to communicate between objects or java should provide.

So ,it means you require and expect from java like something called "default functionality" where you dont want yourself to bother about communication between objects and instatiation.
That's y java has the functionality of default constructor.

If you not provide any constructor means you are using java default constructor way to communicate.
The time you provide any constructor in class ,you deprived of the default functionlaity.because you have defined you own way of constructing object.

Moral is ->
So, default constructor is one of the facility provided by java for devlopers not to bother the object
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
"Default constructor" -> As it's clear by name "Default" means ,if you won't provide any specific value ,you will have some initial value automatically.

Basically , The basic purpose of "Constructor" is "to construct the object or to instatiate".

So ,no arg-constructor and args-construtor is the way to instantiate.
Regardsless of the mechanism you use, the basic purpose will be same.

As every body discussed about default costructor in object class and java is "single rooted hierarchy".

"Singly rooted hierarchy" it's serves a specific purposes like every object in java is child of "Object class".

Just consider for the example ,that what would happend if java has no default constructor?

Suppose ,if you have class

Class A{}
Class B extends A{}
Class C extends B{}

if you look at the above code ,you will find that there is no constructor and we know java is singly rooted hierarchy ,so how these class will communicate with each other.

The only way is either you provide some mechanism to communicate between objects or java should provide.

So ,it means you require and expect from java like something called "default functionality" where you dont want yourself to bother about communication between objects and instatiation.
That's y java has the functionality of default constructor.

If you not provide any constructor means you are using java default constructor way to communicate.
The time you provide any constructor in class ,you deprived of the default functionlaity.because you have defined you own way of constructing object.

Moral is ->
So, default constructor is one of the facility provided by java for devlopers not to bother the object
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: quick class question