-1

I am new to java classes.

I have a two problem here.

Problem 1: I want to create a class called "ClassA", and I want it to be the same as class made by Vaadin called "Component". So when I write

ClassA classA = new ClassA();

It is the same as:

Component component = new Component();

How is that possible?

Problem 2: I want to create "ClassA", I want it to be the same as Vaadin class 'Component', but I also want to be able to add own methods to this "ClassA".

I hope you can help.

LanDanois
  • 65
  • 6
  • 2
    Whats stopping you from inheriting from the class? – Polygnome Sep 21 '20 at 08:10
  • Inheriting from Component? Or from ClassA or ClassB? I want each to be seperate files, I just wrote two examples / problems because I have those two "ideas" in mind. Sorry, if it was confusing. – LanDanois Sep 21 '20 at 08:27

1 Answers1

2

You can extend classes to use their methods and still able to add your own methods to your class (unless the extended class is final).

class ClassA extends ClassB {
    // don't forget constructor
}

But in this case, this won't work because Component seems to be an interface. So you can just implement the interface but have to create all the methods by yourself.

class ClassA implements Component {
    ...
}

This might be a lot of work so try to find an implementation of Component and then your can extend that implementation.

Milgo
  • 2,617
  • 4
  • 22
  • 37
  • Ok, so the first thing works. That was super easy. Thank you so much!!! For the other thing, can you clarify what you mean with try to find an implementation: Should I look online, or where? I think the component class is just abstract. – LanDanois Sep 21 '20 at 08:25
  • @LanDanois implements is used to implement interface not classes nor abstract classes – IQbrod Sep 21 '20 at 08:29
  • The thing that I need from the component class, is the ability to say "View view = new Label();" for example. If that makes sense. I know you can use the component class for that in Vaadin. – LanDanois Sep 21 '20 at 08:29
  • @IQbrod so should I still use "extends"? – LanDanois Sep 21 '20 at 08:31
  • @LanDanois You really need to grab one of the most basic tutorials on inheritance. [This answer](https://stackoverflow.com/a/2399554/1360803) might be a good start. – Polygnome Sep 21 '20 at 08:32
  • That does not quite makes sense. I assume `View` would be your class? You can't assign a Vaadin `Label` to that, as it does not extend your class. – Erik Lumme Sep 21 '20 at 08:32
  • @Polygnome Thank you, I'll read into that. – LanDanois Sep 21 '20 at 08:34
  • @ErikLumme Well, it might also not be my "own" class. For my project, I just need it to say View instead of Component. It is a vanity thing almost, but going forward it just eases the process for me re-writing some android code into Vaadin. – LanDanois Sep 21 '20 at 08:37
  • You might run into issues with that, as it is not possible for one class to extend more than one other class in Java. Say that you create a class `View` that extends `Component`, there is no way to make e.g. a Vaadin `Button` extend `View`. So for any Vaadin classes, or even any of your own extensions to Vaadin classes, you will never be able to assign them to a `View`. – Erik Lumme Sep 21 '20 at 08:47
  • @ErikLumme Yeah, I see now that others have run into "similar" issues. Probably with more dignifying reasons that mine. But I guess it just comes down to Java not allowing for you to import as alias. – LanDanois Sep 21 '20 at 09:02
  • @LanDanois You can not import via alias, but you can have similar named classes from different packages. You can only import one (or better don't import at all) and use the full qualified name (package + Class). E.g. `a.Component a = new b.Component()` – cfrick Sep 21 '20 at 10:17
  • @LanDanois No, it comes down to Java not allowing multiple inheritance of *classes*, only of interfaces. And Vaadins component is a class, not an interface, so if you inherit from that, you can't inherit something else. – Polygnome Sep 21 '20 at 10:20