Calling a subclass method from superclass
When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.
Pet cat = new Cat("Feline",12,"Orange");
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet
To access the methods in the concrete class (Cat
in this case), you need to either declare the variable as the derived class
Cat cat = new Cat("Feline",12,"Orange");
cat.getName(); // OK, getName() is part of Cat (and the superclass)
cat.getColor(); // OK, getColor() is part of Cat
Or cast it to a type you know/suspect is the concrete type
Pet cat = new Cat("Feline",12,"Orange");
((Cat)cat).getName(); // OK (same as above)
((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat
You can even combine the two methods:
Pet pet = new Cat("Feline",12,"Orange");
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK
Java Inheritance: Calling a subclass method in a superclass
I'm very new to java and would like to know whether calling a subclass
method in a superclass is possible.
A superclass doesn't know anything about their subclasses, therefore, you cannot call a subclass instance method in a super class.
where is the proper place to set public static void main.
I wouldn't recommend putting the main
method in the Admin
class nor the User
class for many factors. Rather create a separate class to encapsulate the main
method.
Example:
public class Main{
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);
System.out.println("Hello "+user1.getName());
user1.getLevel();
}
}
How to call subclass method (Java)
Your declared array arr
is an array of type (class) SuperClass objects. Everything in the array must be a thing that is a SuperClass.
Your subclass named SubClass IS a thing of type SuperClass — it is an extension of that class, by adding y
and getY
, but it still conforms to SuperClass. Anything that you can do with SuperClass you can also do with SubClass, so you are allowed to add SubClass objects to the array of type SuperClass.
Where your error is, at arr[0].getY()
, the thing that is in the spot at arr[0]
is, in fact, a SubClass, because you just put it there, but there is no guarantee that that is the case — the only guarantee is that things in the array are of the type SuperClass, and things of type SuperClass don't have a getY()
method.
When you know that a thing is of type SubClass you can cast it, like: (SubClass) arr[0]
, and must parenthesize it to apply the cast before you attach the method call, so it would be ((SubClass) arr[0]).getY()
Casting has it's own potential errors, if you try to cast something to SubClass when that thing isn't actually a SubClass it will cause an error (but that is another discussion).
So the answer to the question "How to call subclass method" is to cast the object to the subclass:
// Direct replacement for your line of code:
System.out.println( ((SubClass) arr[0]).getY() );
// or assign it to another variable first:
SubClass sc = (SubClass) arr[0];
System.out.println( sc.getY() );
How do you call a subclass method in the baseclass method?
Private vs. Protected
private
methods can only be accessed in the class itself, not in any descendants. You cannot have a private abstract
method because this is an inherent contradiction -- if descendants need to implement it then it is not private
.
You want to use the protected
modifier. Per the docs:
The
protected
modifier acts much like theprivate
modifier with the exception that members declaredprotected
can also be accessed within deriving classes.
You want to declare in your BaseClass
that all concretions must implement a WriteStuff()
method. This is an abstract
method meaning that it has no implementation in BaseClass
and must be overwritten.
In your descendants, the implementation of WriteStuff
must be protected
rather than private
.
Code
abstract class BaseClass {
/* .... */
protected abstract WriteStuff(arg1: Item, arg2: number): void;
}
class SubClass1 extends BaseClass {
protected WriteStuff(arg1: SubClass1Item, number: number) {
// Write stuff in the SubClass1 way
}
}
Playground Link
arg1 Type
Your classes don't all share an identical signature because they each have a different type for the first argument arg1
passed to WriteStuff
. You most likely want to use a generic class based on the type of arg1
or some other value.
If SubClass1Item
and SubClass2Item
do not extend Item
then you absolutely need a generic class because the implementations of WriteStuff
in the subclasses would not be assignable to the base.
If they do extend Item
then you will not get typescript errors with the current setup. However there is potential for runtime errors when you are calling this.WriteStuff
from GetOptions()
in BaseClass
. How does the BaseClass
know if the Item
that it has is assignable to the Item
subtype that is expected in SubClass1
or SubClass2
? This is where generics can help.
We make the BaseClass
a generic based on a variable ItemType
. We can require that all ItemType
variables extend a shared base Item
, but we don't have to.
The subclasses are not themselves generic classes. They will extend a version of BaseClass
with the specific ItemType
that they require.
Code
abstract class BaseClass<ItemType extends Item> {
/* ... */
GetOptions(item: ItemType) {
// Get the options
this.WriteStuff(item, 5);
}
protected abstract WriteStuff(arg1: ItemType, arg2: number): void;
}
class SubClass1 extends BaseClass<SubClass1Item> {
protected WriteStuff(arg1: SubClass1Item, number: number) {
// Write stuff in the SubClass1 way
}
}
Playground Link
How do you call a subclass method from a superclass in Java?
The answer for my specific JavaFX subclassing question is over here Subclassing a JavaFX Application. For general subclassing the answers here are quite adequate.
Why is my superclass calling my subclass method?
The answer is that a base class that calls a method that is defined on itself, but also overridden by a subclass, calls the overridden method on the subclass not the method on the base class. For further information see calling an overridden method from base class?. See the below variant of your code and follow the logic as described above.
class A:
def __init__(self):
self.do()
def do(self):
print("do A")
class B(A):
def __init__(self):
super().__init__()
self.do()
def do(self):
super().do()
print("do B")
b = B()
Result:
A
B
A
B
Related Topics
Sorting a Collection of Objects
Why Does the Division of Two Integers Return 0.0 in Java
How to Increase the Jvm Memory
Why Doesn't Java Send the Client Certificate During Ssl Handshake
Clone() VS Copy Constructor- Which Is Recommended in Java
How to Specify Jackson to Only Use Fields - Preferably Globally
What's the Nearest Substitute for a Function Pointer in Java
What Is the Garbage Collector in Java
Using Setdate in Preparedstatement
How to Import a Class from Default Package
How to Decrypt File in Java Encrypted with Openssl Command Using Aes
How to Switch Between Frames in Selenium Webdriver Using Java
How to Use a Custom Serializer with Jackson
Regex for Matching Something If It Is Not Preceded by Something Else