Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I'm trying to extend two classes, the one is the built in ActionBarActivity and the other is MainActivityWithButtons.java , I need to extend the second one (MainActivity..) because I need to access the booleans from it. How do I inherit the booleans?
Posted
Comments
Richard MacCutchan 19-Jul-15 7:40am    
I need to extend the second one (MainActivity..) because I need to access the booleans from it.
That sounds like totally the wrong way to go about it. It would be better if you explained exactly what problem you are trying to solve. If you have some class that needs to get information from another class then you would normally use property getters, or other methods, to access the variables.
Member 11847156 20-Jul-15 0:19am    
The booleans are set to true when one of the buttons is pushed on the first page, then the second activity opens and when you push that button on the second page, it sends an SMS which then depends on the first boolean
Richard MacCutchan 20-Jul-15 3:33am    
Then you should use the Intent.putExtra(String name, boolean value) method, to pass the value to the second intent.

1 solution

1) You cannot extend two classes, for such function you should try using interfaces and implement them. Each interface can in turn extend one class for itself.

2) You cannot inherit booleans, boolean is a type, primitive type.

Instead, you should try instantiating and using the boolean variable, or make it static and call it as,

Java
MainActivity.variable


For this reason, multilevel inheritance has been introduced so that you can use features of 2 or more classes. http://beginnersbook.com/2013/05/java-inheritance-types/[^]

Java
// Example of "what I am saying"
class First {
   // members and functions of First class
}

class Second extends First {
   // members and functions of First and Second class
}

class Third extends Second {
   // members and functions of First, Second and Third class
}


You can use Third and call functions and members inherited from First and Second class also. Even boolean.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jul-15 13:52pm    
5ed.
—SA
Afzaal Ahmad Zeeshan 19-Jul-15 13:56pm    
Thank you, Sergey.
Member 11847156 20-Jul-15 0:00am    
Thanks, I have the boolean set in MainActivityWithButtons and in the second class I have for example:
MainActivityWithButtons mainAct=new MainActivityWithButtons;
boolean breakIn=mainAct.breakin;

The boolean is set to true in the main activity, but the second class isn't using it properly, it's meant to work like this:

In the MainActivity it's set to true
In the second activity, it's got to do something if it's true, but it's not doing what I need it to do
Afzaal Ahmad Zeeshan 20-Jul-15 3:10am    
Then check that value, set it to static and final (so that the child class cannot change the value) but MainActivity can change the value. You can then use that field and change the state of your second activity.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900