How to extend two library class in java class android [duplicate] - android

This question already has answers here:
Extending from two classes
(13 answers)
Closed 9 years ago.
I want to extend two library class files in a java class.How to do this.

You have not given more details about the question.
You can only extend a single class. And implement interfaces from many sources.
Extending multiple classes is not available.
You can use nested classes or inner classes
class A extends B {
private class C extends D {
// A , B , C , D accessible here
}
}
Why Use Nested Classes?
Compelling reasons for using nested classes include the following:
It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
when to use nested classes
You can find more solutions on this link
Edit
This is an answer to you comment. You want to call method of outer class in inner class. This is an example.
class Outer {
void show() {
System.out.println("inside outter show");
}
class Inner{
void show() {
Outer.this.show(); //this is calling Outer class method into Inner class
Example e = new Example(); //create object of another class
e.show(); //call to method
System.out.println("inside inner show");
}
}
public static void main(String[] args) {
Outer o = new Outer();
Inner i = o.new Inner(); //create an object of Inner class
i.show(); //this is calling Inner class method from outside method
}
}
class Example
{
void show()
{
System.out.println("inside example show");
}
}
Output:
inside outter show
inside example show
inside inner show

Unfortunately in JAVA you can only extend a single class that means each Class can only extend one class. you can implement many interfaces but not extend.
however there are ways in which you can sort of surpass it, you can just make the libs public and then include them so you could create an instance and use their functions, you can create an inner class and use it for whatever purposes you need...
you can also create a chain of extension like:
public class A extends Activity
public class B extends A
so B will extend both...sort of
its hared to give you a working solution when we dont exactly know the issue,do you mean adding support libs? adding SDK? or really extending two classes (which is impossible straight forward).
#Aniket gave you an example of how to work around it so to speak...
hope I helped
sorry for the bad news:)

Related

Android, Kotlin: What exactly is called here?

I had Android Studio convert my code for an OnClickListener.
Obviously a lambda is used here. What I don't know is whether the lambda is passed to a function of the View class or to the constructor of the OnClickListener class.
I have already looked in the Android documentation for a method with the name OnClickListener in the View class but found nothing.
If the latter is the case, I don't understand how an instance of the OnClickListener class can be created even though it is an interface.
val onClickListener = View.OnClickListener { refreshListView() }
The class OnClickListener is an inner class of the View class:
https://developer.android.com/reference/android/view/View.OnClickListener
Inner classes are identified by a '.' in their classname, on byte code level they are separated with a '$' instead.
With the invocation as written in your question you create an anonymous class implementing the functional interface and implementing its onClick(View) method. You can do the same in Java with a bit more verbose syntax, but technically the same.

How to redefine library functionality in project?

I have library project that implements most of application functionality, it's like a template of application. Every project that uses this library can redefine some resources, themes and so on. Main case is colors and urls to get information, that this applicatoin would show. But to redefine some code is more problematic. For example there is view that displays information from xml, but xml is different and I need to parse it differently. My current realization is like this.
public class MyView extends LinearLayout {
public setData(XmlData xml) {
//call to helpers static method to get parsed data from xml
ArrayList<Item> items = ParseHelper.getItems(xml);
}
}
So what I need is only change some logic inside ParseHelper. Now I see only one way, to redefine layout.xml to change MyView to ProjectMyView in which I'll change method setData to use another ParseHelper. But it's not good.
Maybe there is some patterns or another ways to solve this?
I think another way to use different classes from library or project is to use reflaction. For example packages in project is differs only by name (com.library.helpers and com.project.helpers) and check for class in project, if exists use it, if no use from library. But I think it will use many resources.
Can anyone share their experience?
You can make MyView as abstract, and let setData as an unimplemented method and forcing all subclasses to implement this method like this:
public abstract class MyAbstractView extends LinearLayout {
public abstract setData(XmlData xml);
}
Them, you library has an class that extends MyAbstractView with the most usual implementation like this:
public class MyView extends MyAbstractView {
public setData(XmlData xml) {
//call to helpers static method to get parsed data from xml
ArrayList<Item> items = ParseHelper.getItems(xml);
}
}
For those which want a different implementation, they just need to also extend MyAbstractView.
Finally, the caller or these objects just need to do something like this:
public void init(MyAbstractView arg, XmlData xml) {
arg.setData(xml);
}

Compound component design problem in Android

I am trying to create a compound component in android. This compound component have 2 inner components. one of them is a custom component (assume CompX ) with some public methods.
And the second component is the plain button
So the compound component looks like the below,
class CompoundComp extends LinearLayout{
private CompX customComp;
private Button comp2;
public void method1(){
------------------------------
------------------------------
}
-----------------------
-----------------------
}
class CompX{
public void methodA(){
----------------------
}
public void methodB(){
----------------------
}
}
Now I am using the Compound Component from a client program as,
class Client{
CompoundComp compoundObj = new CompoundComp();
compoundobj.method1();
}
Now my problem is to access the CompX methods. My known solutions for this are as,
In CompoundComp class create public methods that in turn calls the CompX public methods
Make CompX instance as public in CompoundComp class so that the client can call them directly
Frankly I am not able decide which way to go as I am unable to conclude on the pros 'n' cons of both the solutions.
Someone please suggest me if my solutions are proper one or not. If so which one is the better one to use, if not so please give me some suggestions or clues of solutions.
Thanks
The CompX class is not static, so you can't create a instance of it.
To initialize a inner non-static class you should create it with a object of the outer class.

Using Child Class In Android

Sorry, I'm a newbie to Java and Android...
I've made an app that has a big main activity, I'm wanting to split it up into some child classes to make things easier to read, edit etc.
From my main class I (think) I am instantiating, and calling the child class, from within OnResume :
SetupButtons SetupButtonsObject = new SetupButtons ();
SetupButtonsObject.buildthem();
And in the child class I do:
class SetupButtons extends main {
void buildthem(){
//a load of things to setup buttons
}
}
The code I am using in buildthem() works fine when used in the main class, but is giving a nullpointer exception when used in the child class.
do I need to pass the main context to the child or something?
Many Thanks
Tom
Anything you use in the childclass that is not set there, but set in the main class, you must in some way get from the main class. So yeah, you need some context.
But before you do that: you might want to divide your app in classes that are logical parts of your sollution, as you can read in any OOP description. So not just chop it up in parts because it makes it smaller, chop it up in logical units that are actual good objects.
A quick type, hope I did not make any mistakes here, but see comments in child class.
class Parent{
public static int foo = 1;
public static int bar = 0;
public function somefunction(){
bar = 1;
myChild = new Child();
}
}
class Child extends Parent{
//you can find foo = 1
//but not bar = 0;
}
The Activity class isn't supposed to be instantiated by developer directly, leave this work to the android. If you wish to split it up why do you extend your main activity? You rather need to divide your app in logical parts as Nanne said.
In same class you can call that method directly. Like buildthem();

Difference between file, class and activity in Android

What's the difference between file, class and activity in android?
File - It is a block of arbitrary information, or resource for storing information. It can be of any type
Class - Its a compiled form of .Java file . Android finally used this .class files to produce an executable apk
Activity - An activity is the equivalent of a Frame/Window in GUI toolkits. It is not a file or a file type it is just a class that can be extended in Android for loading UI elements on view
Class -
A class is a combination of methods, variables and data types. Every Java or Android project must have at least one class.
Example:
public class shape{
public void circle()
{
int A,B,radias;
}
}
Activity -
An Activity is an android class. If we want to use an activity class, we must use extend Activity in your android project.
Example:
public class shape extends Activity{
public void circle()
{
int A,B,radias;
}
}
File is a file on the filesystem. Class is a Java class. Activity is a specific java class commonly used in Android.
1) Class is Blueprint of object and you will create as many object you want from same class. You can create new object by “new” keyword. In example below “ArrayList” is class and “obj” is object.
ArrayList<String> obj=new ArrayList<String>
2) Activity :- Every program has some starting point. In android Activity is starting point of any application you made. It is basically a GUI of the app. In android app every activity have to inherent directly or indirectly from Activity Class which is predefined in the android system. So activity is also a class but a special one. So you can say that “Every activity is a class but every class is not Activity”.
3) File :- file is used to store data so you can reuse again when you app start.
An activity is actually a class (click) and if you want to make your own activity you choose this one as parent class.
And the source code of classes is defined in files, actually every class should be described in its own file.
That's some basic knowledge of object oriented programming - you might want to have a look here to find more information

Categories

Resources