I want to access the instance variable x in the super class.
I can't find a syntax for it. Is this even possible?
public class SuperBoss {
int x = 10;
}
public class Boss extends SuperBoss {
int x = 2;
public static void main ( String[] args ) {
Boss b = new Boss();
System.out.println(b.x); //prints out 2, I want it to print out 10
}
I've been trying to use the keyword "super" with no success.
It prints out 2 because your declaration of int x = 2 in Boss is hiding the declaration of x in SuperBoss
Normally you would use the super keyword to explicitly access public fields of the superclass, this however won't work from the static context of the main method. You can define a small helper function that will get you the value of the superclass's x
public class Boss extends SuperBoss {
int x = 2;
public int getSuperX() {
return super.x;
}
public static void main ( String[] args ) {
Boss b = new Boss();
System.out.println(b.getSuperX()); //prints out 2, I want it to print out 10
}
}
As far as I can tell (and I should probably look in the JLS for the details), it is legal to access a shadowed non-private field with super, but only from within an instance method. So you could do this:
public class Boss extends SuperBoss {
int x = 2;
int getSuperX() {
return super.x;
}
public static void main (String[] args) {
Boss b = new Boss();
System.out.println(b.getSuperX());
}
}
Add getSuperX() method in your Boss class:
public class Boss extends SuperBoss {
int x = 2;
public int getSuperX() {
return super.x;
}
public static void main(String[] args) {
Boss b = new Boss();
System.out.println(b.getSuperX());
}
}
Yes, there is a way. Fields have compile-time binding. What you need is a cast to SuperBoss:
public class SuperBoss {
int x = 10;
}
public class Boss extends SuperBoss {
int x = 2;
public static void main ( String[] args ) {
Boss b = new Boss();
System.out.println(((SuperBoss)b).x);
}
}
To do what you want, you need to declare x as protected in the superclass, and not attempt to override it in the subclass:
public class SuperBoss {
protected int x = 10;
}
public class Boss extends SuperBoss {
// int x = 2;
public static void main ( String[] args ) {
Boss b = new Boss();
System.out.println(b.x);
}
}
In practice, however, there is little need for protected data. It can cause almost as many problems as public data. Better is:
public class SuperBoss {
private int x = 10;
protected int getX() {
return x;
}
}
public class Boss extends SuperBoss {
// int x = 2;
public static void main ( String[] args ) {
Boss b = new Boss();
System.out.println(b.getX());
}
}
What is going on is that the instance variable of Boss is hiding the instance variable of the same name in SuperBoss. You can temporarily cast your instance to the supertype class:
public static void main ( String[] args ) {
Boss boss = new Boss();
SuperBoss superBoss = boss;
System.out.println(boss.x); //prints 2
System.out.println(superBoss.x); //prints 10
}
This is not recommended. For encapsulation reasons, your instance variables should not be visible, ie. should be private/protected. And probably should not have the same name as variables in parent classes.
Related
public class MyTest {
public static void main(final String[] args) {
B b = new B();
b.print();
}
}
class A {
private final int x = 5;
protected int getX() {
return x;
}
public void print() {
System.out.println(getX());
}
}
class B extends A {
private final int x = 10;
#Override
protected int getX() {
return x;
}
}
In this example, I need to print subclass value in the parent class.
It is working fine. No issue.
Now it is printing 10.
But I do not want to define that property in the parent class A.
Because in this example this x datatype is very simple. So no issue.
But in real-time I want to use other datatype which may be another Class variable or List<something> which have huge data.
So ultimately I do not wish to store that value in Class A.
Because it is redundant data. It will slow down in my Hibernate thing.
Please let me know, how to achieve this without declaring variable in parent class. But I still need to use subclass variable in parent class.
make abstract your class A and the getX(); method.
public class Test {
public static void main(final String[] args) {
B b = new B();
b.print();
}
}
abstract class A {
protected abstract int getX();
public void print() {
System.out.println(getX());
}
}
class B extends A {
private final int x = 10;
#Override
protected int getX() {
return x;
}
}
and override the toString method in place of your print method
#Override
public String toString() {
return String.valueOf(getX());
}
the final code
public class Test {
public static void main(final String[] args) {
B b = new B();
System.out.println(b);
}
}
abstract class A {
protected abstract int getX();
#Override
public String toString() {
return String.valueOf(getX());
}
}
class B extends A {
private static final int X = 10;
#Override
protected int getX() {
return X;
}
}
you could also define as static your x variable
But as say Andrew Tobilko you can consider also to use an interface if A doesn't represent a stateful entity.
It's certainly the best solution for your case, mix the use of an interface and an abstract class
public class Test {
public static void main(final String[] args) {
B b = new B();
System.out.println(b);
}
}
interface MyInterface {
int getX();
}
abstract class A implements MyInterface{
#Override
public String toString() {
return String.valueOf(getX());
}
}
class B extends A {
private static final int X = 10;
#Override
public int getX() {
return X;
}
}
You need the getX within the parent class, but you don't have information enough to implement this method there.
You can declare this class as abstract and mark the method with abstract as well. Doing that, you are handing the responsibility of method implementation over its subclasses and preventing from parent field declaration.
If the A doesn't describe any state (only actions/methods), you should consider replacing it with an interface. At the current state, it is the case.
You could make the parent class abstract, eliminate the property in the parent class, make getX() abstract, and then leave print() as concrete. Then just use the concrete implementation of getX() in the child class.
I was playing around with classes since I'm learning java syntax and I came across this weird situation. So given a class A and B:
public class A {
public int x() {
return x;
}
}
public class B extends A {
int x = 5;
public int x() {
return x + 2;
}
public static void main(String[] args) {
B b = new B();
System.out.println(b.x());
}
When I execute the main method I get a compile time error that it doesn't find the x variable and it's calling the method from A because the error shows return x instead of return x + 2. Since b's static type is B, why is it looking in A for x?
error: cannot find symbol
return x;
symbol: variable x
location: class A
The class A doesn't know that it will be extended by B, where the x variable will exist.
In order to make this compile, make A.x() abstract and provide implementation within the subclass:
public abstract class A {
public abstract int x();
}
public class B extends A {
int x = 5;
#Override
public int x() {
return x + 2;
}
..
}
When you are creating the Object of class B, it automatically invoke the x() of class A, But x is not initialized in that class. As it is a local variable, it can not be used without initialization. so it is giving an error.
Try the below code, it is working correctly
class A
{
int x =10;
public int x()
{
return x;
}
}
class B extends A
{
int x = 5;
public int x()
{
return x + 2;
}
public static void main(String[] args)
{
B b = new B();
System.out.println(b.x());
}
}
I have the following problem. Am trying to make a polymorphic call and the result would depend on the variable that changes value depending on the underlying class. Tried different things however it doesn't work. Please let me know what should be changed. Problem is that although c.w reads both the local variable w, which is defaulted to 0 and reads the one from appropriate class it always defaults to 0. Here is the code:
class Cycle{
private int w = 0;
public void move(){
System.out.println("Cycle moving");
}
public int wheels(Cycle c){
switch (c.w){
case 1: return 1;
case 2: return 2;
case 3: return 3;
default: return 0;
}
}
}
class Unicycle extends Cycle{
public int w = 1;
public void go(){
System.out.println("Unicycle go");
}
}
class Bicycle extends Cycle{
public int w = 2;
public void go(){
System.out.println("Bicycle go");
}
}
class Tricycle extends Cycle{
public int w = 3;
public void go(){
System.out.println("Tricycle go");
}
}
public class TestCycle {
public static void ride(Cycle c){
c.move();
int now = c.wheels(c);
System.out.println(now);
}
public static void main(String[] args){
Bicycle b = new Bicycle();
ride(b);
Unicycle u = new Unicycle();
ride(u);
Tricycle t = new Tricycle();
ride(t);
}
}
Your problem (well one of them) is that you are redefining the class variable 'w' in each of your subclasses. Define it one as a member of 'Cycle' and have each subclass set it correctly in their constructors.
class Cycle{
protected int w;
public void move(){
System.out.println("Cycle moving");
}
public int wheels(){
return w;
}
}
class Unicycle extends Cycle{
public Unicycle() {
w = 1;
}
public void go(){
System.out.println("Unicycle go");
}
}
Or you can define an abstract method called 'wheels()' in the superclass and override it in the subclasses. It's a matter of taste.
the wheels method should be more like
public int getWheelCount(){
return this.w;
}
You invoke it on the instance itself, you don't need to pass an argument. If the current instance is a Tricycle, the method will return 3, etc...
Since Cycle.w is private, it's not visible from its inheritors. This means that for example Tricycle.w it's not the "same" variable, and it's not visible in Cycle (that's why you always get 0). You have to make Cycle.w at least protected, then remove w from all subclasses, and set its value in each subclass's constructor to what you want.
It's probably not the answer you are looking for, but the following works. Please give more details on what you are trying to do.
public abstract class Cycle {
protected int nWheels;
protected String goText;
// no constructor.
public void go() {
System.out.println(goText);
}
public int wheels() {
return nWheels;
}
}
...
public class Unicycle extends Cycle {
public Unicycle() {
nWheels = 1;
goText = "Unicycle go";
}
}
Note that I made Cycle abstract because I don't want it to ever be instantiated.
EDIT:
public static int getNumberOfWheels(Cycle cycle) {
return cycle.wheels();
}
which is obviously not very useful since a simple call to cycle.wheels() would do the same as calling this function.
I'm not sure why you want to avoid constructors. Maybe you should write the exact question you are trying to answer.
I have two classes that extend the same abstract class. They both need the same constant, but with different values. How can I do this? Some example code to show what I want to do.
abstract class A {
public static int CONST;
}
public class B extends A {
public static int CONST = 1;
}
public class C extends A {
public static int CONST = 2;
}
public static void main(String[] args){
A a = new B();
System.out.println(a.CONST); // should print 1
}
The above code does not compile because CONST is not initialized int class A. How can I make it work? The value of CONST should be 1 for all instances of B, and 2 for all instances of C, and 1 or 2 for all instances of A. Is there any way to use statics for this?
You can't do that.
You can do this, however:
abstract class A {
public abstract int getConst();
}
public class B extends A {
#Override
public int getConst() { return 1; }
}
public class C extends A {
#Override
public int getConst() { return 2; }
}
public static void main(String[] args){
A a = new B();
System.out.println(a.getConst());
}
If a constant has a variable value, it's not a constant anymore. Static fields and methods are not polymorphic. You need to use a public method to do what you want.
Ex
class A () {
class A(int a, int b) {
}
}
class B extends A {
int m;
int n;
class B()
{
getInput(); // i wanna invoke this method first before calling super(). But it does not allow in Java. How to work around this ?
super(m,n);
}
public void getInput() {
Scanner scanner = new Scanner(System.in);
m = scanner.nextInt();
n = scanner.nextInt();
}
public static void main () {
B b = new B();
}
}
You can force your super class to run a method at the beginning of its constructor and then override that method in the subclass. Many frameworks have a "setup" type method that you can override to accomplish such things.
public class A {
protected int a; // 'protected' so subclass can see it
protected int b;
public A() {
setup(); // Runs whatever setup method is implemented, even in subclasses
}
protected void setup() { /* nothing */ } // 'protected' to be overridden by subclass
}
public class B extends A {
public B()
{
super();
}
/**
* When A's constructor calls setup(), this method will run.
*/
#Override
protected void setup() {
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt(); // Stores value in A's protected variable.
b = scanner.nextInt();
}
}
Depending on the specifics of the classes you're writing, this is where you might have multiple constructors, public or protected methods for setting values, etc. This is where Java is fairly flexible. As the comments below indicate, this isn't a very good practice in constructors, but I'd need more context to figure out how to accomplish what you're asking.
You could chain multiple constructors together as jbrookover alluded to in such a manner. Sligtly convoluted though:
class A () {
public A(int a, int b) {
}
}
class B extends A {
int m;
int n;
public B()
{
this(new Scanner(System.in));
}
private B(Scanner scanner) {
this(scanner.nextInt(),scanner.nextInt())
}
private B(int m, int n) {
super(m,n)
this.m = m;
this.n = n;
}
public static void main (String ... args) {
B b = new B();
}
}