-1

I am beginner and trying to learn by studying material online. I just draw a diagram and want to show you so that you please put me on the right path.

Here is Class Diagram

Question-1: Is that drawn correctly
Question-2: How to implement this diagram into Java Code

Trying to build SiteTemplate that has 3 sub classes e.g. (1) different elements like modal, buttons, combo box, table etc (2) Java Script element like error checker and messages (3) all URLs that'll be used in project so that if the site move from one server to another we just change URLs and it start work again

I am trying to that if I inherit SiteTemplate class then have access to all methods of sub classes and their child classes

Best Regards

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1
    1) if you don't tell us what it should represent, how should we know if it is correct? 2) It is advantageous to write some code yourself first – Lino Feb 14 '21 at 16:59
  • @Lino I edited my question and added what exactly looking for. Please get me advice about class diagram and if it is correct then I can try some code – Java_Beginner Feb 14 '21 at 17:22

1 Answers1

2

if I inherit SiteTemplate class then have access to all methods of sub classes and their child classes

No, the fact SiteTemplate is inherited directly or indirectly by other classes does not allow SiteTemplate to have access to the methods of these classes.

In fact this is in the reverse direction, the child classes inherits the public/protected methods of the inherited class(es).


If SiteTemplate correspond to an element of a site your generalizations are right, but what I said above still apply. May be also SiteTemplate is an interface and in this case the generalizations are realizations. In Java you use extends for generalizations and or implements for realizations.

SiteTemplate by default does not know the classes inheriting/realizing it, to make it explicitly knowing them is a bad architecture.


If you want to say a SiteTemplate is composed by any number of ProjectURL and JSElement and HTMLElement the generalizations are wrong and you can use aggregation (or even composition) :

enter image description here

that allows SiteTemplate to access to the elements composing it, and then to apply on these instance the public operations their classes define.

In Java they are attribute, and because the number of instances are unknown you use collections.


Warning, do not name class at plural, so ProjectURL etc whatever these classes have several instances, this is why in my answer I do not use plural

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Thanks for your guidance. `In fact this is in the reverse direction, the child classes inherits the public/protected methods of the inherited class(es).` Please correct me how can I make SiteTemplate as abstract and access its all child or sub child classes methods – Java_Beginner Feb 14 '21 at 18:02
  • @Java_Beginneryes SIteTemplate can be abstract (explaing why *Template*) – bruno Feb 14 '21 at 18:07
  • sorry for my poor English. I am assuming Template as Graphic or HTML design of the whole website. – Java_Beginner Feb 14 '21 at 18:08
  • @Java_Beginner for the rest I edited my answer, please refresh it. Is ok my English is not perfect too ;-) – bruno Feb 14 '21 at 18:10
  • Please advise how can I access methods of e.g. Table class from main SiteTemplate class? I appreciate you make class diagram better by adding aggregation and inheritance – Java_Beginner Feb 14 '21 at 18:13
  • @Java_Beginner a class is not a method and an instance of a class is not a method of the class too. As I say in my answer if you have access to an instance you can apply a method of its class( or mother class) while this is compatible with its visibility – bruno Feb 14 '21 at 18:16
  • can I suppose we are in front of a XY problem ? I mean rather than to ask help about the *real* problem you imagined a solution and you ask help about that solution. – bruno Feb 14 '21 at 18:22
  • You get the jest what I am looking for `if you have access to an instance you can apply a method of its class( or mother class) while this is compatible with its visibility`. Can you please advise how can I achieve this goal? – Java_Beginner Feb 14 '21 at 18:41
  • @Java_Beginner if *a* values an instance of *A* which defines the method *m* getting no parameter then just do `a.m()` . I did not imagined you was at that level, better you look at tutorial/course about Java. Do not use stackoverflow for questions about basis of the language, this is not the right way – bruno Feb 14 '21 at 18:51
  • Its mean I become disappointed here!! – Java_Beginner Feb 14 '21 at 19:15
  • @Java_Beginner do not get that like that. The problem is just you cannot ask for the right questions if you do not have enough bases, so difficult for us to answer you and finally to help you – bruno Feb 14 '21 at 19:20
  • can you please give me some example code of this diagram to study and understand. My main problem is English language that am trying hard to understand it – Java_Beginner Feb 14 '21 at 19:24
  • @Java_Beginner what is your nationality / natal language ? – bruno Feb 14 '21 at 19:27
  • Persian Language – Java_Beginner Feb 14 '21 at 19:28
  • I do not have links for Java nor UML in your language (I am French). It is in English you will find the more inputs. If you are student you have courses I suppose, and of course *google* to find material. I think you have first to learn basis of Java, *then* to look at UML and for it do not hesitate to use a UML modeler managing Java to help you. Sorry but I don't know how to help you more in a general way – bruno Feb 14 '21 at 19:42
  • `of course google to find material` what I search specifically – Java_Beginner Feb 14 '21 at 21:28