• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

quick class question

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

}

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesnt everything in Java extend the Object implicitly? (pure OO language).
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant was, since every class extends the Object class, eventually the default "Object" constructor would be used.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic