In general, the question is. As you know, if class B is inherited from class A, then class B will have access to all methods from class A, (except for the case with private). In other languages ​​everywhere. And why in Android, inheritance is somehow differently understood. That is, if class B is inherited from class A, and class A is started at the beginning of the program, then if we turn from class A to class B, then class B will do the same as class A. That is, it will also start and do everything that It was written in class A. It's wrong. With inheritance, it only becomes possible to use methods from the inherited class. And here it turns out that Class B identically performs everything that is written in class A.

In Class A, a code is written that displays a picture, Class B, it turns out, it also starts to display a picture when it starts up.

Here is the CODE http://ideone.com/10A8K8

  • 2
    Show me an example of code that you think is illogical. And then too many unnecessary words - DreamChild
  • 3
    @xTIGRx, and now it would be nice if you specifically explained the code you mentioned, it is in this code that you think is incorrect / illogical / incomprehensible, because from this code it is rather difficult to understand what exactly you meant by A and class B, and how is your question related to this code? - DreamChild
  • 3
    > That's why in this situation, the private setTXT method starts in class B ??? so you call it inside class A, inside it is available - DreamChild
  • four
    @xTIGRx I’m telling you a second time - you call this method only from class A. You do not call him anywhere in class B, do not fool your head - DreamChild
  • five
    @xTIGRx and now for the third time - you are nowhere (here it’s straight generally, nowhere at all. Absolutely nowhere) do you call the setTXT method inside class B. You will (again, just in case, repeat it) call inside class A within its public method onCreate. And you can call onCreate method of class A from class B derived from A, because this method is public. So I hope you understand? - DreamChild

3 answers 3

To begin with, as has been repeatedly noted, the methods of the parent class A , being called even from the derived class B , behave the same. So the behavior you specify is correct and takes place in Java both under Android and all other platforms. And also in all other languages.

The comments sounded constructive question: what to do? How to make the setTXT method not run in a derived class, but only in the base class?

Here are the options. The easiest conceptually is to make the setTXT method not private , but protected , and override (by default, virtual methods in Java methods) this method in a derived class:

 public class B extends A{ @Override protected void setTXT() { } } 

It can be done more simply, but less beautifully: check the exact class type in setTXT :

 private void setTXT(){ if (getClass() /* динамический тип */ != A.class /*статический тип*/) return; Toast.makeText(getApplicationContext(),"Привет",Toast.LENGTH_SHORT).show(); } 
  • The only normal answer. Here are the options. The easiest conceptually is to make the setTXT method not private, but protected, and override (by default, virtual methods in Java methods) this method in a derived class: public class B extends A {@Override protected void setTXT () {}} - Andro

Suppose there are two classes "A" and "B", "B" is inherited from "A", then the created object "b" will also be the object "a", "b" will have all the methods "a" .

  • If the object "b" calls the print () method, which is in the class "A", but which is not in "B", then the print () method from the class "A" will be executed.
  • If the “b” object calls the print () method, which is not in the “A” class, but which is in “B”, then the print () method from the “B” class will be executed.
  • If the “b” object calls the print () method, which is in the “A” class and in the “B” class, the print () method from the “B” class will be executed. In this case, in the print () method of class "B", you can call print () from class "A" (that is, from the superclass) via "super" ( link ).

All this applies not to the OS, but to the programming language, so learn Java!

ps the print () method is public, to understand the meaning of modifiers (and their absence) it is better to read a java book ...

  • @DreamChild, but the onCreate method cannot be made private, and how can one proceed to call the setTXT method ONLY IN CLASS A. How can this be done? - Andro
  • 3
    @xTIGRx: Will, will. Java access modifiers (and in all other languages ​​I know) work at the lexical level, at compile time, and control what is visible and what is not visible. At runtime, they do not exist and do not play any role. (Well, well, some role play for reflection'a.) - VladD
  • 3
    > If you are such an expert in OOP, then tell me, why do you think you need access modifiers? Less emotion, please. You probably do not really understand the principle of data encapsulation in OOP (one of the basic principles in this concept) and continue to stubbornly be mistaken about how it works, so @ProkletyiPirat described these elementary things to you, and did not mock you. Read more about data encapsulation, and then everything will probably fall into place. - DreamChild
  • four
    @xTIGRx, you are too emotional ... take it easy, have a smoke, or drink tea with cookies, in general, get distracted a little ... As for age, then on the Internet (and even more so on hashcode) it is not an argument, but if I’m really interested, I’m 25 at the moment ... I’ve already answered your question, but apparently you’re stuck and went into a stupor, digress a little, and then look at your code again, you’ll be very funny, when you realize the reason for your problem :) - ProkletyiPirat

Modifiers private, protected, public are the determinants of the scope, and not the scope of execution. You, as I understand it, perceive them as limitations on the area of ​​implementation. If this were the case, what kind of reaction would you expect to create class B? And if there are many of these methods? They made a direct heir, and all calls to private methods in the ancestor were reset, or what?

And what if we bring class B to class A?

PS Yes, but what happens in C ++ in this situation? It seems to me the same thing, but it would be interesting to see the code that confirms your logic in C ++.

  • one
    in C ++, everything will also depend on the presence of modifiers (or their absence). If the method in the parent is virtual, it will be the same as in java; if it is non-virtual, an error will appear about the absence of the method, the difference is that in java all methods are virtual by default, but not in C ++. - ProkletyiPirat
  • one
    I think there will be no mistake anyway. More precisely, I know :-) - Chad