Adding specific values for inherited fields - java

I am having some trouble with inheritance (Student here). I need to be able to utilize 1 inherited private field for each subclass I make. Obviously subclasses cannot have access to inherited fields however when a new object is created that inherited private field is a part of that object. For my purposes though each subclass needs to have it's own specific value for that inherited field. My first attempt looks something like this:
Public class A {
private int x = 0;
public A(int n) {
x = n;
}
public int useX() {
return x;
}
}
Public class B Extends A {
int n = 1;
public B() {
super(n);
}
useX(); // Return 1?
}
Public class C Extends A {
int n = 2;
public B() {
super(n);
}
useX(); // Return 2?
}
However my professors tell me that I could also be using a setter method inside of my Super class to create that new field, and from there I am confused. Can anyone help point me in the right direction?

An ordinary Java Bean provides public accessors and mutators (aka getters and setters) for it's fields. However, you could provide a protected setter. Something like,
public class A {
private int x = 0;
public int getX() { // <-- the usual name.
return x;
}
protected void setX(int x) {
this.x = x;
}
}
Then your subclasses can invoke that setter
public class B extends A {
public B() {
super();
setX(1);
}
}
And then B.getX() (or B.useX() if you really prefer) will return 1.

Related

How to initialize inherited values, and have abstract methods c++?

This is kinda multiple questions in a single question. First off, is there a way to initialize inherited values in C++? In other words, this is what I mean in java :
// Class X
public class X {
int num;
public X(int num) {
this.num = num;
}
}
//Class Y
public class Y extends X{
public Y() {
super(5);
}
}
My other question is how would I create an abstract method in C++?
Again, java example :
// Class x
public abstract class X {
int num;
public X(int num) {
this.num = num;
}
public abstract void use();
}
// Class y
public class Y extends X{
public Y() {
super(5);
}
#Override
public void use() {
}
}
Thanks.
First: You want to learn about initializer lists. For your first code snippet, the appropriate code would be
class X {
int num;
public:
X(int t_num) : num(t_num) { } // We can use initializer lists to set member variables
};
class Y : public X {
public:
Y() : X(5) { } // No need to have anything in the body of the constructor
};
For the second, in C++ you can always declare functions without providing a definition, which is very similar to defining an abstract function in Java. If your intent is to do run-time polymorphism, you'll also want to make them virtual functions:
class X {
int num;
public:
virtual void use() = 0; // The = 0 indicates there's no implementation in X
};
class Y : public X {
void use() override { } // No need to redeclare virtual
// override keyword not necessary but can be clarifying
};

In Java how to refer subclass variable without declaring that variable in parent class?

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.

A Java interface to enforce by design the output type of unknown methods of its implementors?

At present, I've defined an empty interface X that is implemented by some other interfaces (just to identify them elsewhere).
All interfaces that implement X may provide as many public methods as they wish, but I want to enforce by design/by architecture, that all of them (note again that they are unknown to X) will return a type that is derived from the same abstract class Y.
Ist their any way I can do this in Java?
In the following example, X should enforce that only types derived from Y are returned by U and V.
public interface X {
// I'm empty at present.
}
public interface U extends X {
public A getA();
public B getB(String bIn);
}
public interface V extends X {
public C getC(Integer cIn);
public D getD(); // Compile should fail!
}
public class A extends Y {
}
public class B extends Y {
}
public class C extends Y {
}
public class D {
// D does *not* extend Y.
}
There is no way to enforce this with the java type system. You would therefore be left with:
reflection
customised static analysis
code reviews & developer education
I would stay away from reflection and static analysis. You haven't said what problem you are trying to solve with this, so it's difficult to give any alternate approaches.
I agree with #fge that this sounds like an XY problem, but I think you might be able to get something to work at compile-time.
You want to place a requirement on every method of a type, but Java only lets you specify that there exist some methods satisfying some requirements on a type, so you will have to refactor U and V.
In the set-up, I've made X specify that any implementors must provide a way to return a Y descendent. I've also specified that Y is an abstract class.
interface X {
Y getY();
}
abstract class Y {
}
Then, I looked at your interfaces U and V, and their methods U#getA(), U#getB(String), V#getC(Integer), V#getD(). All of these methods can be put in their own class.
class UA implements X {
public A getY() {
...
}
}
class UB implements X {
private final String s;
public UB(String s) {
this.s = s;
}
public B getY() {
...
}
}
class VC implements X {
private final Integer integer;
public VC(Integer integer) {
this.integer = integer;
}
public C getY() {
...
}
}
// COMPILE-TIME ERROR
class VD implements X {
public D getY() {
...
}
}
Now, anything that implements X must provide Y. The problem now is that UA, UB, VC, and VD can offer other methods. You've said you only want them to provide methods that return Y. To get around this, you can replace X with a final concrete class, which only provides a single constructor that you control.
Replace X with YFactory (everywhere in the code)
interface YFactory {
Y getY();
}
Now, specify X as a concrete class which only has one constructor:
final class X {
private final YFactory yFactory;
public X(YFactory yFactory) {
this.yFactory = yFactory;
}
public Y getY() {
return yFactory.getY();
}
}
All together:
final class X {
private final YFactory yFactory;
public X(YFactory yFactory) {
this.yFactory = yFactory;
}
public Y getY() {
return yFactory.getY();
}
}
abstract class Y {
}
interface YFactory {
Y getY();
}
class A extends Y {
}
class B extends Y {
}
class C extends Y {
}
class D {
// D does *not* extend Y.
}
class UA implements YFactory {
public A getY() {
return null;
}
}
class UB implements YFactory {
private final String s;
public UB(String s) {
this.s = s;
}
public B getY() {
return null;
}
}
class VC implements YFactory {
private final Integer integer;
public VC(Integer integer) {
this.integer = integer;
}
public C getY() {
return null;
}
}
class VD implements YFactory {
public D getY() {
return null;
}
}
Now you know that any X only has methods that return Y.

How can my subclass properly inherit a static variable

public abstract class SuperClass {
public int x, y;
public static int z;
}
I want every subclass of SuperClass to have the static variable z. Naturally z will contain a different value for each subclass. I'm hoping to avoid defining z in every subclass, since it's going to be a functional dependancy of values x and y; Is this possible?
Unlike instance variables that are "one per instance", static variables are not "one per subclass" - they are "one per declaring class". In other words, subclasses of SuperClass share SuperClass.z, but they cannot "override" it on a class-by-class basis.
It does not mean that you cannot implement it yourself: on way to make your own per-subclass storage of integers is adding a static Map<Class,int> zs to SuperClass, with optional functions for accessing the data:
public abstract class SuperClass {
public int x, y;
private static Map<Class,Integer> zs = new HashMap<Class,Integer>();
protected static int getZ(Class c) {
Integer res = zs.get(c);
return res == null ? -1 : res.intValue();
}
protected static void setZ(Class c, int v) {
zs.put(c, v);
}
}
class SubClassOne extends SuperClass {
public int getZ() {
return SuperClass.getZ(SubClassOne.class);
}
}
class SubClassTwo extends SuperClass {
public int getZ() {
return SuperClass.getZ(SubClassTwo.class);
}
}
Probably the best way to do this is to have a z() method or similar in the abstract class, and override the method in the subclasses you want.
Example:
public abstract class SuperClass {
public int x, y;
protected int z() {
return 42; // protected so only subclasses can see it - change if required
}
}
public class SubClassOne extends SuperClass {
public void foo() {
// do something...
int z = z();
// something else...
}
}
public class SubClassTwo extends SuperClass {
#Override
protected int z() {
return 1;
}
// use z() accordingly
}
if i understand you correctly then do
public abstract class SuperClass {
public int x, y, z;
public SuperClass(int z) {
this.z = z;
}
}
then any class that extends this class inherits z;

polymorphic call depends on variable

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.

Categories

Resources