+ 1
A bit confused with inheritance. How to assign values in sub class for string variables which are declared in super class?
Eg: String name, size, sound; are declared in animal class. how to assign values for these variables in elephant class which extends animal?
2 ответов
+ 3
first you to check if it's a private or public variables if public you can access it directly by using the supper.animal=elephant; but if it's a private variables you need to use a getter and setters
+ 1
If the variable is declared, use super keyword. example:Parent class Animal has variable String name = "dumbo"; Elephant class would, generally in the constructor, call super.name; This is a bad example however, since what you'd really want to do is just String name; in the animal class. In the elephant class, just do name = "dumbo", usually in the constructor.