• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts about my classes

 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49860
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49860
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49860
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49860
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic