Any way to extend two or more classes in java?

I know that Java does not allow us to distribute more than one class. I know that interfaces also exist, and I do not want to use them this time. Is there any trick or workaround for extending multiple classes in Java? If yes, also provide an example code.

0
source share
12 answers

You can easily handle interfaces and composition.

The question is, why do you ask such a thing? Why don't you want to use the interfaces "at this time"?

You should know that Java allows only one implementation inheritance. You spent seven months at SO, so you should probably know how to use the search function. The question has been asked here several times.

Be more creative. There is no reason that a JPanel should be Observable. JPanel is intended for rendering. I agree that his model data may want to be observable. Be sure to do this with the composition.

+3
source

No, unlike C ++, you cannot extend multiple classes, but you can implement multiple interfaces.

+3
source

NO.

go interfaces . There are no multiple inheritance in Java.

Multiple inheritance can cause a diamond problem.

JAVA misses a lot of rarely used, poorly understood, confusing features of C ++, which in our experience bring more grief than good. This primarily consists of operator overloading (although it has a method overload), multiple inheritance, and extensive automatic enforcement _ Dr. James gosling

+2
source

Multiple inheritance in java is usually done by implementing multiple interfaces. You cannot extend multiple classes. A possible workaround may be to use some kind of composition of the object, for example, for aggregation.

+2
source

Java does not support this for classes. You can use several interfaces. You may find that the problem with multiple inheritance in java

You can use inner classes like

  public class class1 extends class2{ class Inner extends class3{ } } 

SEE HERE

+1
source

maybe it helps

  class One{ void oneMethod(){ } } class Two extends One{ void TwoMethod(){ } } class Abc extends Two{ @Override void oneMethod() { // TODO Auto-generated method stub super.oneMethod(); } @Override void TwoMethod() { // TODO Auto-generated method stub super.TwoMethod(); } } 
+1
source

Multiple inheritance adds complexity with slight advantages, so it is not in java. Why is multiple inheritance not allowed in Java or C #?

One of the ways that I can think of is to write your program that requires multiple inheritance in a language that supports it, for example, C ++, and then make your Java code interact with the exit from this program, for example, using files or databases.

+1
source

Answer No. But you can do this if you help:

A extends B
B extends C

therefore A extends B and C

0
source

Use interfaces instead. Here's what it looks like in the code:

 public Class ClassName implements Interface1, Interface2 { Methods, fields etc... } 
0
source

To avoid Java does not support multiple inheritance through classes, but supports interfaces. Thus, you can use the Association. eg.

 Class A {} Class B {} Class C implements SomeInterface { A a; B b; // setter getter for a and b and other methods } 
0
source

You can use the search feature.

 class Vehicle { T <T> lookup(Class<T> klazz) { return map.get(klazz); // typically } } interface Flyable { void fly(); } Vehicle x; Flyable f = x.lookup(Flyable.class); if (f != null) { f.fly(); } 

This separates the classes. This requires a runtime check.

It uses delegation: local members implementing the lookup'ed class / interface.

Advantage:

You can get a class from Vehicle that can fly (Flyable) and dive (Diving). Flying can be implemented by an instance of a common class. You can even dynamically create opportunities (add wheels to the boat).

This is often better than implementing multiple interfaces, where fly() will either be a copy of the code or delegate to the Flyable field (with a common implementation class).

In particular, when using multiple interfaces / classes, you otherwise risk code on objects of the base class (Vehicle here) with cases and unsafe drops, forgetting if (x instanceof Boat) .

0
source

I'm going to upset a lot of people, saying yes, you can - sort of .

This, of course, is not easy, and you should use reflection, but here you will find an example that allows you to dynamically expand one object to another. Obviously, as soon as you can do this, you can also expand another one.

In essence, it uses the Proxy class to intercept method calls and direct them to one or two of two objects - this implements A overrides B This is not quite what you are looking for, but it can be easily configured to detect the methods that exist in both classes and call them both, thus achieving A extends B dynamically - a kind of.

Please note that you need to read the Dynamic Object Adapter section using the Dynamic Proxies section of the link.

0
source

Source: https://habr.com/ru/post/983436/


All Articles