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

Doubts about my classes

Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hi everybody!
I have 3 classes: Person, Costumer and Employee.
Person is my superclass and Costumer and Employee extends Person.

But, I can't create a new object Person. So, Person will be an abstract class, right? But, all the methods are concrete... I can't put any method that is abstract AND I need the constructor of the superclass. What I'm supposed to do? Create 1 constructor in Costumer and another one identical at Employee?

I guess that I did not understand very well the concept of abstract class: an object of that kind (Person) can't be created, but the abstract class will have a constructor?

Thanks!
Paras Jain
Ranch Hand

Joined: Feb 26, 2005
Posts: 137
I couldn't fully understand what you were asking for but, as you said.

But, all the methods are concrete... I can't put any method that is abstract

is not right completely.
If you are talking about child classes you are right.
In Customer and Employee you can put all concrete methods if you want them to be concrete class.

In Person class you can have all the abstract methods, all the concrete methods or mix of both.

Create 1 constructor in Costumer and another one identical at Employee?


There is no relation between the constructors of the costumer and employee, if costumer and employee are siblings.


Paras Jain
SCJP 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I think you have got the concept of abstract classes correct and haven't realised it. You can make Person abstract even if it has no abstract methods.

Make Person abstract, with a constructor, and put constructors in both your subclasses.
Suggest you have exactly the same parameters for all the constructors. Something like this:That should work.
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Yes!
Thanks, to both of you.
I did not got the right concept of constructor. I guess that I thought that if the class have the constructor it will create a new object of that type. Such a dumn!

Thanks folks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Andre Brito:
Such a dumn!

Thanks folks!
Dumn my foot! You haven't done anything daft. The concept of inheritance and where the constructor goes is complicated to understand. Don't go calling yourself a dumn.


And we are only too pleased to be able to help.
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hehe. Thanks CR!
But, only to finnish: an abstract class can't create any objects of that type, but it can have a constructor, that will be used by subclasses, right?

Cumps!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Andre Brito:
Hehe. Thanks CR!
But, only to finnish: an abstract class can't create any objects of that type, but it can have a constructor, that will be used by subclasses, right?

Cumps!
Yes, that is correct. If your constructor takes any parameters, it must be called by subclass constructors.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hi cambell

what happen to the no argument constructer
of person class. any why this constructer is going to be
called by the constructer of customer class . when customer is instintiated
if this is going to happen then abstract class is going to be instantiated

whats will happen in this scenario

parent class should have a no argconstructer all the extending class consructor will call this no arg constructor

regards
amir
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
No, I would not have a no-argument constructor in Person. I would put common fields in Person and set them up from the Person constructor, then classes like Customer would use super calls in their constructors.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hi campbell
iam getting Implicit super constructor person() is undefined. Must explicitly invoke another constructor

this is the code i tried

abstract public class person {

int age;
int sex;

public person(int x,int y)
{
this.age=x;
this.sex=y;
}


}

public class customer extends person {

String name="name";

public customer(int x,int y ,String name)
{

this.age=x;
this.sex=y;
this.name=name;
}

}


looking for your replies
[ January 15, 2008: Message edited by: Amirtharaj Chinnaraj ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The Customer class's constructor should look like this:



That "super()" line calls the constructor you defined in Person, and lets it do the work it's designed to do. Then you can do additional work.

Every constructor has to use "super(something)"; if it doesn't, the compiler inserts "super()", a call to the superclass's no-argument constructor. If there isn't one -- which is the case here -- the code won't compile. That's what your error message is telling you.


[Jess in Action][AskingGoodQuestions]
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
thanks Ernest

but my doubt continues if when ever the customer instance is created super(x,y); is going to create an instance of person since the person is an
abstract class how could this be possible because abstract class cannot be instantiated

if person is not an abstract class this can be possible

looking for your replies
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

You don't create a Person object, just your Customer object.

See "super" as a form of include. When you call super(x, y), it will only execute the code in Person(int x, int y)*, which in turn will call super()** which will call the code in of the Object() constructor. This phenomenon is called constructor chaining.



* Any initializer blocks will also be run; these are parts of code that exist within the class outside any method but inside { and }.

** If you don't specify a call to "this(...)" or "super(...)", the compiler automatically adds a "super()" for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
thanks rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts about my classes