What's the proper convention to access ancestor and parent methods from down an inheritance chain?
For example, methodA() resides in the base ancestor class and methodB() resides in the parent class. If I'm in a child/subclass that extends parent (which in turn extended the ancestor/base class), what is the proper way to access methodA()?
Obviously super.super.methodA() is not allowed.
What does work is super.methodA(), this.methodA() and simply calling methodA() on it's own.
Which of the above three cases is the 'correct' way to call methodA() that resides in the ancestor class?
If methodA() is defined only in the grandparent class, and isn't overridden in the parent class or child class, then simply calling methodA() in the child class will correctly call the inherited method.
Accessing a classes grandparent methods is not allowed. See Why is super.super.method(); not allowed in Java? form more information.
Related
I know how these stuff flow but I want to know when we make the object of child class it calls the parent class no arg constructor first. But i want to know the internal thing.
The parent class constructor needs to be called before the subclass constructor. This will ensure that if you call any methods on the parent class in your constructor, the parent class has already been set up correctly.
I want to extend a class A and call the constructor of the class A from the child class B with the super() method. Also should the class A only be instantiated via the child class B. I can do that very simply by making class A abstract. But I read that I should only declare classes abstract, when they have at least one abstract method. Is there another way of making class A only be instantiated by the child class B by calling the super() method?
You should use abstract if there are certain implementations your child class needs, especially if you don't want the parent class to be instantiated ever!
However, in your case, I don't think there are any methods that need to be implemented or inherited, so you can just use protected.
Good luck!
Is there any other way to access child object's variable when the object is referred by its parent class other than casting the object to child class?
class parent{
int parentData;
}
class child extends parent{
int childData;
}
parent obj =new child();
obj.childData =10 ; //is getting error
((child )obj).childData=10; // is working fine
is there any other way to access child class variable where child class object is pointed by parent class ?
scenario
If my parent class has 2-3 child class that are differ with parent class by only one variable and i tried to refer all child class object with parent class.In some point i need to get child variable what should i do ? do i need to redesign ?
The parent class should contain only behavior that is common to all child classes.
If what you are trying to implement in the parent class is indeed common behavior you might want to look in to the Template method pattern.
If what you are implementing is different behavior over the child classes, do not try to shoehorn it into the parent class but implement it in the child classes. If these implementations share common parts, you could extract this to a common method either in the parent or a helper class.
How come parents started to know thier childs in java? That's not possible.
I have define super class who's member variables are private and functions are public. I want that super class' methods to be accessible in the child class, but not from the outside of that class.
In other words, super class' methods become private in the child class so that they can not be accessed from outside of the class. Is this behavior possible or not?
I think you are searching for the protected modifier. protected methods can be accessed from classes in the same package and extending classes.
Another way is the inversion of control using the template method pattern: The base class defines behavior in a non-abstract method using abstract methods the child classes have to overwrite - so that the child classes only have to extend the specific parts and don't have to care about the rest.
technically java doen't have a modifier for this use case but protected make you come closer but allow to access within package.
I have a class (let's call it A) that is extended by several children class (B, C, D, etc.).
In each child class, there are specific methods that I'd like to be accessible from an instantiation of the parent class A.
I tried to declare A as an abstract class, and to declare each child class methods inside as abstract. Then I implemented these methods in their own class but it seems that each child class must implement every method of the parent class. However, I can't do this.
Would you have an idea for this issue?
In each children class, there are specific methods that I'd like to be accessible from an instantiation of the parent class A.
That's not the point of inheritance. Even if you could do this, it shows that your design is broken.