If Hand is self defined abstract class , then it implements itself as template? it doesn't make too sense. what's your take on the above declaration
From docs.oracle.com:
"It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract."
The declaration you have only names two interfaces: Comparable, and Serializable. Since Hand is an abstract class, it doesn't have to actually implement those interfaces, but any non-abstract class that extends Hand must do so.
The page: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html has more good info/explanation.
If class implements some generic interface, passing itself as parameter, it means the class implements some methods with itself as parameter. So in case of Comparable it tells, that class MUST have method compareTo to compare itself with another instance of the class.
Makes sense to me. It means that you can compare two hands.
Related
I'm a beginner in Kotlin (and Android for the matter) and I saw that the first opening line of the MainAcitivty class is:
class MainActivity : AppCompatActivity() {
The braces after AppCompatAcitivty mean that this class has a constructor right? I searched inside this class and didn't find any constructor, so where is the constructor of AppCompatActivity?
Is it hidden in a super class that AppCompatActivity itself extends from? If so how can I track it?
Thanks in advance
If no constructor is defined in Java, the class defaults to having an implicit constructor with no arguments and an empty initializer. It's the same in Kotlin...your class definition here for MainActivity has no constructor defined, so it implicitly has a constructor with zero arguments (although you can still define an initializer, but don't do this for an Activity--see below). If you subclassed MainActivity (provided it was marked open, you would have to call the implicit constructor like this:
class MyOtherActivity: MainActivity()
Also in Java, the default implicit constructor automatically passes back to the super constructor. If you follow the inheritence of AppCompatActivity all the way back up to Activity, you'll see that none of the classes have a defined constructor, so they are all empty.
Activities in Android must always have a single no-argument constructor because the OS instantiates them by reflection. You will never instantiate an Activity directly. Do your set-up in the lifecycle callbacks.
It means the MainActivity extends the AppCompatActivity class. This is a classic inheritance. See example here.: https://kotlinlang.org/docs/tutorials/kotlin-for-py/inheritance.html
But of course, it has a constructor, a default one.
Is there any case such that: both class A and class B can extend AsyncTask at the same ? Or else, should it be one class C that extends AsyncTask?
Any number of classes can extend AsyncTask at the same time (or any other class). Extending means that the new class inherits fields and methods of super-class and can override super-class methods. The same applies to AsyncTask as well.
I'm new to android programming how to extend two activity . In my case I'm using ActionBarActivity
im already extends the a class for some functionalities how shall i extend two activities my class
any example code will be more useful for me
This is not much of a Android problem (as Activity is class as any other), it's the way Java works.
Java doesn't support multiple inheritance, so classes can only extend one other class.
class A extends B{
}
Even in this case:
class A {
}
class A extends another class - Object - but it is automatically implied without having to specify it.
If you want to ensure some functionality from several sources you will have to use interfaces and the implements keyword:
class A extends B implements C,D,E {
}
Can I implement OnClickListener and SurfaceHolder like this :
public class MainActivity extends Activity implements SurfaceHolder.Callback,OnClickListener
Yes, in Java a class can implement many different interfaces.
This compensates the lack of multiple inheritance in Java, as opposed to e.g. C++ where multiple inheritance is allowed.
I have created an abstract class which extends the Activity Class.
I want to extend it in an activity which extends TabActivity Class, how can I achieve that?
Get the TabActivity and abstract class Functionality at the same time.
I do realize that multiple inheritance is not possible in Java, but can I avoid extending TabActivity Class and still use getTabHost()?
10x :)
I don't think you can achieve this with this design. If your abstract class extending SomeOtherActivityClass and new Class need to extended SomeotherActivityClass (through inheritence) and TabActivityClass, that may confuse dalvik to choose which activity at runtime and it doesn't make sense. You may need to reconsider your design.
yes sure, you can use. By using following snippet:
TabHost host=(TabHost)findViewById(id of tabhost);
It's better way to create another class that extends Application class. Or use singleton pattern and create class with all functionality that you're need.