开发者

Java Classes versus C++ Classes

开发者 https://www.devze.com 2023-03-14 06:37 出处:网络
I just started learning to develop for the Android, and I come from a strictly C++ background. I found that Java does not support Multiple Inheritance, but do we really have to create a seperate .java

I just started learning to develop for the Android, and I come from a strictly C++ background. I found that Java does not support Multiple Inheritance, but do we really have to create a seperate .java file for every new Activity?

I have a TabActivity, with 3 more Activitys that make up the tabs. I want to create a List View on one of the tabs, but I am starting to get confused. Do I need to make another .java file that extends ListActivity? I can see even a relatively small appl开发者_开发技巧ication becoming extremely large if I am going to need to create a seperate .java file for every activity.

SIDE NOTE

If I want to add the ListView to one of my tabs, how would I do that? Nothing I have found thus far mentions how to add new activities to other Activities.


The other answers give good points about interfaces and that is probably more what you are looking for. However, for further information, no you don't have to create a new .java file for every new class, there are alternatives. However, keep in mind that more classes is not necessarily a bad thing. Your options are...

Nested Class:

public class A {

    public/private class B {

    }

}

Instances of B can access private variables of A but cannot be constructed without an instance of A (this is an approach 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 they do not require an instance of A in order to be constructed. Instances of B can be instantiated anywhere if B is declared public, or only in A's methods 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 has no name and functions the same as a nested class (e.g. can access private variables). It's an alternative form of writing nested classes that is sometimes shorter but leads to very strange syntax.


Java doesn't support multiple inheritance on abstract or normal classes but on interfaces only.

What you can do is create abstract classes that extends a particular Activity and keep creating abstract classes until you have a class that inherit all your activity features.

Alternatively, is have a class that inherits multiple interfaces.


If you use an IDE to do this, the overhead of creating lots of file is much smaller.

However there are a few ways to have many classes in the same file. This includes a) using inner/nested classes b) anonymous classes c) enums d) package local classes.

The only thing you cannot do is easily if spread the definition of a class across multiple files.


Java does not support Multiple Inheritance

Workaround is you use interfaces

but do we really have to create a seperate .java file for every new Activity

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


Java doesn't allow multiple inheritance. But it does allow implementing multiple interfaces. You don't need to create a seperate file for each class (though it's a good practice). You can encapsulate sub-classes in the main class of your file.

ListView can be simply extended and you can use the corresponding extended class in the xml etc.


Java does kind of support multiple inheritance, if you just make the base classes abstract and call them interface instead of class.


You can simply declare a ListView in XML and call it in the Activity, no need to have a separate ListActivity, just for ListView. It's when your whole Activity is a ListView.

And as per the concerns about more classes, it's not a bad thing. More classes means more object oriented, not strictly though (Everything has a limit).
Multiple Inheritence was considered not good due to problem of diamond of death. So the work-around was to use Pure Virtual Classes known as Interface in Java (Interface means a normal class in Objective-C though).
I suggest you to read HeadFirst Java second edition, for better grip on terminology and their use. It's a good book for beginners.

0

精彩评论

暂无评论...
验证码 换一张
取 消