Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance doubt

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've class Contrato (abstract class) and classes ContratoParticular and ContratoConvencao that extends the former; can another class (eg class Person) have a reference to class Contrato, like:

thanks in advance
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes

You are welcome
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not instantiate an instance of an abstract class, you must subclass it and then implement it's abstract methods in your sub class.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both for answering!

You can not instantiate an instance of an abstract class, you must subclass it and then implement it's abstract methods in your sub class.


my class diagram: (well, more or less)
______________________
Contrato
______________________
- tipo : String
...
______________________
+ recebido()
...
______________________

i dont want to instantiate Contrato (i know i cant), but was great if i could get a way to reference this abstract class in order to access (with a getter):
tipo
so i ask again: i can do it, right?
thanks again
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define a variable of the abstract type and make it refer to an instance of a concrete type.

All that compiles and runs when other classes call with an instance of ContratoParticular or ContratoConvencao. The Person class never knows or cares which one it is. Person can even call methods defined on Contrato without ever knowing what concrete class it has. The beauty and power of abstraction at work!

BTW: Translate Contrato for me. It has a very nice ring to it. I hope it's not something mundane, but this being a programming problem I don't have high hopes.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Translate Contrato for me. It has a very nice ring to it. I hope it's not something mundane, but this being a programming problem I don't have high hopes.

contrato is a portuguese word and means contract (i guess): something agreed between two entities.

All that compiles and runs when other classes call with an instance of ContratoParticular or ContratoConvencao. The Person class never knows or cares which one it is. Person can even call methods defined on Contrato without ever knowing what concrete class it has. The beauty and power of abstraction at work!

you mean this?


since we are at this subject, by any chance do you know how to map inheritance with hibernate? because i'd have a Q...
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another set of examples:

List list1 = new ArrayList();
List list2 = new LinkedList();
Map map1 = new HashMap();
Map map2 = new TreeMap();
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think he means that you can do something like this:

In fact, the above code illustrates how you can declare a reference variable with the base class type and assign an instance of the subclass to it. This technique is actually very common. I often use code similar to the examples above using classes from the Collections framework.

Layne

Layne
[ February 16, 2005: Message edited by: Layne Lund ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've playing with all this for a while and i noticed that, when i query, i've to mention the right subclass. Is this allways like this?
(i'm using hibernate)
here Utente extends Pessoa
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
i've playing with all this for a while and i noticed that, when i query, i've to mention the right subclass. Is this allways like this?
(i'm using hibernate)

I'm not sure what you mean by "I've to mention the right subclass". Which line of code in your example illustrates the idea you are trying to convey here?

Layne
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all Pessoas have a getApelido and setApelido, then you could say

I never used Hibernate, but it looks like the question would be the same whether or not you use it.

It comes in handy when you have two different types of Pessoas that have the same method that returns different things.

For example, you may have two types of Animais, Gato and Cavalo. Gato and Cavalo extend Animal. Gato and Cavalo each have a method called fala() but they return different things. Try putting a Gato and a Cavalo into a List and then call
System.out.println( animal.fala());
[ February 16, 2005: Message edited by: Marilyn de Queiroz ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
by any chance do you know how to map inheritance with hibernate?
For your specific case, check out this very similar thread.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by "I've to mention the right subclass". Which line of code in your example illustrates the idea you are trying to convey here?

i refer to my query - hql (hibernate query language queries objects), so i thought, maybe naively that i could query the superclass, but i get an error and have to query the concrete class:

Utente is a subclass of Pessoa

It comes in handy when you have two different types of Pessoas that have the same method that returns different things.

For example, you may have two types of Animais, Gato and Cavalo. Gato and Cavalo extend Animal. Gato and Cavalo each have a method called fala() but they return different things. Try putting a Gato and a Cavalo into a List and then call
System.out.println( animal.fala());

is quite agreable, for once, seeing someone using our language

i had two classes: like User and Technician (Utente and Tecnica) and i decided to put name, other name ,lastName, telephone and mobile phone in another class (Person). First i had a one to one relation between Utente or Tecnica and Person. After i tried inheritance, making Utente and Tecnica is-a Person. Now i believe this isnt quite useful, since i only inherit those accessors for name, otherName etc, but the other classes have their own (diferent) methods and so i cant use polymorphism in a complete way... so i got back to my original design.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic