Creating an array of subclasses of a class - java

I'm trying to store all subclasses of A which are constructed by super() in the array child (in this case B). All the children are in the same package. This is my approach but I don't know how the class could store itself inside an array or pass itself as argument to super().
class A {
static int i = 0;
A[] child = new A[10]
int someval;
A(int val){
someval = val;
child[i] = ???;
i++;
}
}
class B extends A{
B(){
super(val);
}
}
Is this even Possible? With my approach B will only be added when a new B() is created? Is it possible to get a complete array without creating a new object?

public class A {
private static final List<A> instances = new ArrayList<>();
public A() {
instances.add(this);
}
public static List<A> getInstances() {
return instances;
}
}
Now A.getInstances() can be called whenever you like, and will contain instances of anything that extends A.

Related

Call base method on generic object of derived class

Trying to add a base interface with method so all derived classes have to implement the method or use default method. What's the best way to going about getting this method callable? See comment in code block below.
public interface IA{}
public interface IB{
public Integer doWork();
}
public interface IC extends IB{
}
class B implements IB{
Integer doWork(){
return 2;
}
}
class C extends B implements IC{
#Override
Integer doWork(){
return 7;
}
}
//What do I need to do to cast clazz to an object so I can call the derived class' doWork method?
private Integer newClient(Class<T> clazz){
((B) clazz).doWork();
}
Ended up finding a solution:
B.class.cast(clazz);
As for how to ensure you call the derived class' method that overrides the base, that is a native behavior of Java.
Example Program:
public class Foo {
static class A {
int get() { return 0; }
}
static class B extends A {
#Override
int get() { return 1; }
}
public static void main(final String[] args)
{
A a = new A();
B b1 = new B();
A b2 = new B();
printA(a);
printA(b1);
printA(b2);
}
public static <T extends A> void printA(T bObj) {
System.out.println(bObj.get());
}
}
Output:
0
1
1
Note that the output returned from b2::get()::int is the same as b1::get()::int, even though b2 is type A and b1 is type B. This is because even though we only have a reference to the A class in b2, the object implementation is still B.
It seems that you only want to know how to instantiate the Class. Assuming it has a default constructor you can do it this way:
private Integer newClient(Class<B> clazz){
try {
((B) (clazz.getConstructor().newInstance())).doWork();
} catch ...
}

Java inheritance fields [duplicate]

This question already has answers here:
Is there a way to override class variables in Java?
(17 answers)
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 5 years ago.
I am not able to understand the following output.
I don't know why the output is 10, I think the line A a = new B() creates a new instance of class B, I think the result should be 20
class A {
int i = 10;
}
class B extends A {
int i = 20;
}
public class MainClass {
public static void main(String[] args) {
A a = new B();
System.out.println(a.i);
}
}
Why this works like this .. please explain.
First, see Hiding Fields (emphasis added)
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different
In other words, this isn't "inheritance" since you're actually hiding A's i behind B's i, and you are using a reference object of A, so you are getting its fields. If you did B b = new B(), you would see 20, as expected.
If you expect true overrides, try using methods.
class A {
public int get() {
return 10;
}
}
class B extends A {
#Override
public int get() {
return 20;
}
}
See
A a = new B();
System.out.print(a.get()); // 20
If you really want to see both at once, see this example.
class A {
int i = 10;
}
class B extends A {
int i = 20;
#Override
public String toString() {
return String.format("super: %d; this: %d", super.i, this.i);
}
}
And
A a = new B();
System.out.print(a); // super: 10; this: 20
In java you cannot override an instance variable. The output you are getting is expected. In Java you can only override instance methods and not instance variables.
If you want 20 as an output you may use getter methods over those instance variables.
class A {
int i = 10;
int getI() {
return i;
}
}
class B extends A {
int i = 20;
int getI() {
return i;
}
}
public class MainClass {
public static void main(String[] args) {
A a = new B();
System.out.println(a.getI());
}
}
Polymorphism is not applicable for fields in Java.Evaluating Variables decision is taken at compile time so always base class variables are accessed.
Because you define 2 variables: one in the subclass B, and one with the same name in superclass A.
A a = new B();
a.i; // refers to A.i
If you cast the A to a B, it will access B.i:
System.out.println(((B)a).i);
I think you need to use 1 variable:
class A {
int i;
public A() {
i = 10;
}
}
class B extends A {
public B() {
i = 20;
}
}
public class MainClass {
public static void main(String[] args) {
A a = new B();
System.out.println(a.i); // will print 20
}
Member variable i is already defined in class A.
In order to achieve what you are looking for, change the class B as shown below:
class B extends A {
public B() {
i = 20;
}
}

Superclass or Generic Type

I need to be able to understand which method (listed below) would be more beneficial to use in a Java environment.
My problem is thus: I am creating a class in which I am going to use for holding multiple objects of the same type, however if I want to use it in the future, I want to be able to pass different objects to a different instance of Array2D. Its name is Array2D and contains private instance variables named rows and columns. However, what I want to do is be able to pass any object (when you instantiate it) to this class and be able to return the same type with methods inside of Array2D.
Method 1
Create Array2D using generic types. https://docs.oracle.com/javase/tutorial/java/generics/types.html
Method 2
Create a superclass that can be extended from to pass straight into Array2D.
Example of Method 1:
public class Array2D<T> {
private int rows;
private int columns;
private T[] t;
public Array2D(int rows, int columns) {
this.rows = rows;
this.columns = columns;
}
public T[] returnSomething() {
return t;
}
}
Example of Method 2:
Arrayable.java
public class Arrayable {
//all of my variables for arrayable class
}
Example.java
public class Example extends Arrayable {
//more stuff
}
Array2D.java
public class Array2D {
private int rows;
private int columns;
private Arrayable[] arrayable;
public Array2D(Arrayable[] arr) {
this.arrayable = arr;
}
public Arrayable[] returnSomething() {
return arrayable;
}
}
Main.java
public class Main {
public static void main(String args[]) {
Example ex1 = new Example();
Example ex2 = new Example();
Example[] ex3;
ex3[0] = ex1;
ex3[1] = ex2;
Array2D a2d = new Array2D(ex3);
Example[] finish = a2d.returnSomething();
}
}
The only problem I see with Method 2 is that for any class you want to give to Array2D, it has to extend Arrayable. Oh, and the fact that it takes double the time to do. Input??
If Method 1 is the way to go in this situation, please provide an example. I'm basically new to generic classes and setting them up. Thank you!
From what I understood, you want it to be generic for all types of objects, but when you instantiate it, it should only accept subclasses of the declared type. If this is the case, use generics. After you instantiate it, it will automatically accept subclasses of the defined type.
Basically, use your first method. Then change your main like this:
public class Main {
public static void main(String args[]) {
Arrayable[] classArray = {
new SubClass1(),
new SubClass2()
};
Array2D<Arrayable> a2d = new Array2D<>(classArray);
Arrayable[] array = a2d.returnSomething();
}
}

Why subclasses does not override the value of their superclass variable?

I have three classes as following, class b and c are extending class a. I am wondering why the code is not reading the value of b and c variables.
public class a{
protected int myvalue = 1;
}
public class b extends a{
private int myvalue = 2;
}
public class c extends a{
private int myvalue = 3;
}
body of my main method
ArrayList<a> myList= new ArrayList();
myList.add(new b());
myList.add(new c());
for(int i =0;i<myList.size();i++)
System.err.println("value is:" + myList.get(i).myvalue);
output
1
1
From Oracle website:
Within a class, a field that has the same name as a field in the
superclass hides the superclass's field, even if their types are
different. Within the subclass, the field in the superclass cannot be
referenced by its simple name. Instead, the field must be accessed
through super, which is covered in the next section. Generally
speaking, we don't recommend hiding fields as it makes code difficult
to read.
You are shadowing your field myvalue and not overriding it, I believe something like this will do what you want
public class a{
protected int myvalue = 1;
}
public class b extends a{
public b() {
myvalue = 2;
}
}
public class c extends a{
public c() {
myvalue = 3;
}
}
Also, please don't use Raw Types
// ArrayList<a> mylist = new ArrayList();
ArrayList<a> mylist = new ArrayList<>(); // <a> on Java 5 and 6

Set and get a static variable from two different classes in Java

Lets say I have 3 Classes: A, Data, and B
I pass a variable from class A which sets that passed variable to a private variable in class Data.
Then in class B, I want to call that specific variable which has been changed.
So I do
Data data = new Data();
data.getVariable();
It will then return null, since in class Data I initialize variables to nothing (ex: int v;), and I think that class B is initializing a brand new class and resetting the values to default, but I don't know how to fix this.
I know that the variable is setting properly because in class A if I do data.getVariable() it will print the variable that was set.
Class A:
Data data = new Data();
int d = 1;
data.setVariable(d);
Class Data:
private static int b;
public void setVariable(int s)
{
b = s;
}
public int getVariable()
{
return b;
}
Class B:
Data data = new Data();
private int v;
v = data.getVariable();
System.out.println(v);
This will print out 0 instead of the actual value
When you instantiate a Data object in class A, and instantiate another Data object in class B, they are two different instances of the Data class. They both instantiate d to 0 by default. You then call setVariable on the instance in class A and pass it the value of 1; but the instance in class B remains in 0. In order to change the value of the instance in class B, you would need to call setVariable on the instance in class B.
What it seems like you're looking for is a static member. Static members are the same across all instances of the same class. Just put the static keyword before the method(s) or field(s) that you want to use it. Static members and fields are typically accessed using the name of the class in which they are declared (i.e. MyClass.doMethod()). For example:
Class Data (updated):
private static int b;
public static void setVariable(int s)
{
b = s;
}
public static int getVariable()
{
return b;
}
Class A:
Data.setVariable(d);
Class B:
v = Data.getVariable();
System.out.println(v);
Editing - my first suggestion was to use static for variable b, and the author changed his question adding that suggestion.
It fixes what you are trying to do. I write the example in code that compiles:
public class Demo {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.doWhatever();
b.doSomethingElse();
}
}
class Data {
private static int b;
public void setVariable(int s)
{
b = s;
}
public int getVariable()
{
return b;
}
}
class A {
public void doWhatever() {
Data data = new Data();
int d = 1;
data.setVariable(d);
}
}
class B {
Data data = new Data();
private int v;
public void doSomethingElse() {
v = data.getVariable();
System.out.println(v);
}
}

Categories