[CaesarJ] Missing extends relation in the source code?

pabloarias
Erstie
Erstie
Beiträge: 15
Registriert: 1. Okt 2009 09:05

[CaesarJ] Missing extends relation in the source code?

Beitrag von pabloarias »

Hi,

I have some trouble understanding some of the pieces of code provided in the slides about caesarJ.

In slide 23 (lecture 8 ) the implementation of the base HierarchyDisplay is provided:

Code: Alles auswählen

cclass HierarchyDisplayBase {
cclass Node {}
cclass CompositeNode extends Node { ... }
cclass Connection {
void positionNode() {...}
}
void foo(Node n) {
Connection c = new Connection();
}
}
And then two derived classes are implemented:

Code: Alles auswählen

cclass HierarchyDisplayExt1 extends HierarchyDisplayBase {
cclass Node { int maxwidth; ... }
void foo(Node n) { … n.maxwidth … }
}

Code: Alles auswählen

cclass HierarchyDisplayExt2 extends HierarchyDisplayBase {
cclass Connection {
void positionNode() { … super.positionNode(); …}
}
}
My questions is:
Why can positionNode() in HierarchyDisplayExt2.Connection call super.positionNode(), even though it doesn't extend HierarchyDisplayBase.Connection?
Shouldn't Connection in this case just override the Connection class implemented in HierarchyDisplayBase and not extend it?
Why isn't here an "extends super.Connection" needed?

Benutzeravatar
sproksch
Computerversteher
Computerversteher
Beiträge: 346
Registriert: 15. Apr 2004 17:56

Re: CaesarJ

Beitrag von sproksch »

You are right, if you read this code as Java code, but you cannot compare Java and CaesarJ... CaesarJ introduced some new concepts compared to classic OOP. I strongly recommend you to have a look at the exercise about CaesarJ and to try it...

Staying on a more theoretical point of view: Can you explain the term "virtual class" in the context of CaesarJ?

pabloarias
Erstie
Erstie
Beiträge: 15
Registriert: 1. Okt 2009 09:05

Re: [CaesarJ] Missing extends relation in the source code?

Beitrag von pabloarias »

I think I understand the main use case and purpose of CaesarJ:
View a set of related classes as the main unit of modularity instead of a single classes.
Virtual classes are nested classes inside of such a family and they behave just like methods, they are properties of the cclass, can be overriden and inherited.
You can extend an entire family of classes, adding functionality and solving the problems of the multiple variation of the normal inheritance.

Now, returning to my question, doing the CaesarJ exercise helped me to understand the main semantics, but I still have doubts about the code presented the slides.
In the Arithmetic Expressions Task, the base family is implemented as a set of classes representing arithmetic expressions containing evaluation and construction functionality, e.g the Add Class contains the methods eval() and setLeft(), etc.

Now, by extending the base family with the formatting functionality, you do this:

Code: Alles auswählen

public cclass FormattedExpressions extends Expressions {
  
   public cclass Add{
      public String format(){
           //Do formatting
         }

    }

}
It is clear that the virtual class Add in FormattedExpressions doesn't override the Add implementation in the base family (it still has to have the evaluation and construction functionality), but only extends it by the method format().

Which takes me to the conclusion: All virtual classes in a cclass inherit IMPLICITLY from their implementations in the super family, only new functionality can be added (just like in single class inheritance) and all other methods are always inherited. The sentence "virtual classes can be overriden" confused me at the beginning, because you just can't override an entire virtual class, but only its methods, just as normal inheritance.

This answers my question of the super call above.

But now a new question arises:

Why is in the slides the explicit inheritance: "Connection extends super.Connection" needed, when Connection implicit inherits from super.Connection?
Isn't this a redundancy?

I hope I'm on the right way and I'm not confusing anybody... :?

Benutzeravatar
sproksch
Computerversteher
Computerversteher
Beiträge: 346
Registriert: 15. Apr 2004 17:56

Re: [CaesarJ] Missing extends relation in the source code?

Beitrag von sproksch »

The code of l8p23 does compile and does semantically what you just described.

I think p24 just enhances this example with implicit information that you normally do not need to write down (but that are also not incorrect)... it is the same in Java, you can provide "this." but in most cases you don't have to.

I think both "this." and "extends super.XY" are only added at p24 for the purpose of making the transition to p25/26 clear.

Antworten

Zurück zu „Archiv“