Java classes compared to C ++ classes

I just started to learn to develop for Android, and I proceed from strictly C ++ background. I found that Java does not support Multiple Inheritance, but do we really need to create a separate .java file for each new action?

I have TabActivity, three more actions that make up the tabs. I want to create a list view on one of the tabs, but I'm starting to get confused. Do I need to make another .java file that extends ListActivity? I see that even a relatively small application becomes extremely large if I need to create a separate .java file for each action.

FEEDBACK

If I want to add a ListView to one of my tabs, how do I do this? Nothing that I have found so far does not mention how to add new actions to other actions.

+4
source share
7 answers

Other answers provide good interface tips, and this is probably more than what you are looking for. However, for more information, no, you do not need to create a new .java file for each new class, there are alternatives . However, keep in mind that more classes are not necessarily bad. Your options ...

Nested class:

public class A { public/private class B { } } 

Instances of B can access private variables of A, but cannot be built without an instance of A (this is an approach that is often used for button handlers, etc.)

Nested static class:

 public class A { public/private static class B { } } 

Instances of B cannot access private variables of A, but instance A is not required to create them. Instances of B can be created anywhere if B is declared public or only in methods A if B is declared private.

Anonymous class:

 public class A { private void setupLayout() { ... button.addClickListener(new ActionListener() { public void actionPerfored(ActionEvent e) { handleClick(); } }); } } 

This strange syntax creates a class that does not have a name and functions in the same way as a nested class (for example, it can access private variables). This is an alternative form of writing nested classes, which is sometimes shorter, but leads to a very strange syntax.

+1
source

Java does not support multiple inheritance in abstract or regular classes, but only on interfaces.

What you can do is create abstract classes that extend a particular Activity , and continue to create abstract classes until you have a class that inherits all of your activity functions.

In addition, there is a class that inherits several interfaces.

+1
source

If you use the IDE for this, the overhead of creating a large number of files is much less.

However, there are several ways to have many classes in one file. This includes: a) the use of inner / nested classes; b) anonymous classes; c) transfers; d) a package of local classes.

The only thing you cannot do is easy if you extend the class definition to multiple files.

+1
source

Java does not support multiple inheritance

The workaround is to use interfaces

but do we really need to create a separate .java file for each new action

No, if your activity can be performed in an existing class or can be subclassed in an existing class. Java is built around creating classes for each activity.

+1
source

Java does not allow multiple inheritance. But this allows you to implement multiple interface s. You do not need to create a separate file for each class (although this is good practice). You can encapsulate subclasses in the main class your file.

ListView may just be extend ed, and you can use the corresponding extended class in xml, etc.

+1
source

Java does support multiple inheritance if you simply abstract the base classes and call them interface instead of class .

0
source

You can simply declare a ListView in XML and call it in Activity , you do not need to have a separate ListActivity , only for ListView . This is when your Activity is a ListView .

And due to concerns about more classes, this is not bad. More classes means more object-oriented, not strictly (everything has a limit).
The multiple inheritance was considered bad due to the problem of the death diamond. So the workaround was to use the Pure Virtual Classes , known as Interface in Java (an interface means a regular class in Objective-C).
I suggest you read the second edition of HeadFirst Java to better understand the terminology and its use. This is a good book for beginners.

0
source

All Articles