aspose file tools*
The moose likes Beginning Java and the fly likes inheritance doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance doubt" Watch "inheritance doubt" New topic
Author

inheritance doubt

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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


java amateur
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Yes

You are welcome
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
Another set of examples:

List list1 = new ArrayList();
List list2 = new LinkedList();
Map map1 = new HashMap();
Map map2 = new TreeMap();


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
or

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061


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 ]

Java API Documentation
The Java Tutorial
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Feb 08, 2004
Posts: 1281
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance doubt
 
Similar Threads
Saving Detached Object
Hibernate: Mapping Inheritence
jsp:usebean | NoClassDefFoundError
array of object
hibernate session: to close or not to close