i am wondering what happens with object variables in abstract classes in Java. For example if have this abstract class:
public abstract class BaseClass{
private int[] myNumbers;
public Baseclass(int length){
myNumbers = new int[length];
}
public boolean isOne(int index){
return myNumbers[index] == 1;
}
}
and i have this real class which extends the BaseClass:
public class ArrayClass extends BaseClass{
private int[] myNumbers; //i have to define it again?
public ArrayClass(int length){
super(length); //does this affect my array? I don't think so
}
public void setValue(int index, int value){
if(!isOne(index))
myNumbers[index] = value;
}
}
I want to define basic operations in my BaseClass and do some other stuff in my normal ArrayClass. Because i need an array in my BaseClass i have to define one to work with it in the different methods (obviously).
But in my ArrayClass which extends BaseClass i have to define another array. I am not sure why this is and if it needs to be this way? I hope you understand what i mean. For example i could utilize the BaseClass a second time for this normal class:
public class ArrayClass2 extends BaseClass{
private int[] myNumbers;
public ArrayClass2(int length){
super(length);
}
public int getValue(int index){
if(!isOne(index))
return myNumbers[index];
else
return 1;
}
}
The myNumbers array needs to be protected, not private in order to be accessible from within a sub class.
Read more: https://www.tutorialspoint.com/java/java_access_modifiers.htm
Java has four access types. You can read about them here.
If you want to expose the field in your base class to its children, you can use protected modifier.
Related
I have a super class named TestSuper
public class TestSuper {
int a = 0;
}
and I have 2 sub classes named TestSub and TestSub2 that extend TestSuper
public class TestSub extends TestSuper{
int a=1;
}
public class TestSub2 extends TestSuper{
int a=2;
}
in my main class i created a method that takes in a type TestSuper and returns the a value of it and in the main i display it on the console
public class Main {
public static void main(String[] args){
System.out.println(test(new TestSub())+" "+test(new TestSub2()));
}
public static int test(TestSuper b){
return b.a;
}
}
but the output is "0 0" instead of "1 2", what do I do?
You need to cast the reference so say which one you want.
public static int test(TestSuper b){
return b instanceof TestSub ? ((TestSub) b).a :
b instanceof TestSub2 ? ((TestSub2) b).a :
b.a;
}
If this seems needlessly complicated, it is. You should use polymorphism instead.
public class TestSuper {
int a = 0;
public int getA() { return a; }
}
public class TestSub extends TestSuper {
int a = 1;
public int getA() { return a; }
}
public class TestSub2 extends TestSuper {
int a = 2;
public int getA() { return a; }
}
public static int test(TestSuper b) {
return b.getA();
}
First understand the difference between hiding and overriding: https://docs.oracle.com/javase/tutorial/java/IandI/override.html
Then create a getter method in the base-class which you can override in the subclass.
You can look into the theory behind this, and then do the only reasonable thing -forget about writing such kind of code.
In good OOP you consider your fields to be part of your "secret" internal implementation. You don't use fields of sub classes in the super class context. Period.
You are even very conservative about making a field protected in the superclass and to use that in subclasses.
When you call test method like this:
test(new TestSub())+" "+test(new TestSub2())
You use upcasting. Upcasting seperates interface and implementation for an object. But for seperating interface and implementation and achieving true implementation in polymorphism, you must use polymorphic structures. The instance variables aren't polymorphic. Because of this, actually you call a variable which is in TestSuper class.
Only instance methods are polymorphic.
Data data = new Data("path"); //I read data from excel and save. This code is in Main.
public abstract class Generator{
public abstract double[][] generate();
//here I need reference - data
}
public class GeneratorA extends Generator{
public double[][] generate(){
//first implementation - I want to work with data
}
}
public class GeneratorB extends Generator{
public double[][] generate(){
//second implementation - I want to work with data
}
}
What I need is passing reference (data) to abstract class Generator. I can pass reference in constructors of GeneratorA/GeneratorB but I have more child classes and it is inefficient. Is any way how to pass reference data to abstr. class Generator? I just want to inherit reference from class Generator...
Thanks!
You must add a constructor to your abstract class. Then you just have to call super(data) in subclasses constructor.
See the code bellow:
public abstract class Generator{
Data data;
public Generator(Data data) {
this.data = data;
}
public abstract double[][] generate();
//here I need reference - data
}
}
public class GeneratorA extends Generator{
public GeneratorA(Data data) {
super(data);
}
public double[][] generate(){
//first implementation - I want to work with data
}
}
public class GeneratorB extends Generator{
public GeneratorB(Data data) {
super(data);
}
public double[][] generate(){
//second implementation - I want to work with data
}
}
An abstract class can have non-abstract methods and constructors.
public abstract class Generator{
Generator(....)
{
//set here
}
public abstract double[][] generate();
//here I need reference - data
}
You could use the constructor to set the values in the subclass.
OR
If for some reason you don't want to add a constructor/non-abstract method to the abstract class Generator ,you could add one more class which extends Generator and the other subclasses could extend this new class.
You could write a method in the new class for setting values and use it.
It seems from your description you need the same data reference in all Generator objects in this case you can store data as a static field in the abstract class and access in objects of sub classes like:
public abstract class Generator{
static Data data;
public static void setData(Data data){
Generator.data=data;
}
public abstract double[][] generate();
}
Data data = new Data("path");
Generator.setData(data);
public class GeneratorA extends Generator{
public double[][] generate(){
//here you can work with data..
}
}
I have a base class
public class base
{
//some stuff
}
and several subclasses
public class sub1 extends base
{
static int variable;
}
public class sub2 extends base
{
static int variable;
}
etc
The static int variable exists in every subclass because I store in it information that is characteristic for every subclass. But it would be better if there was a way to move static int variable to base class in the way that it still will be different for every subclass.
In the way that it is now I am repeating myself, when adding some another subclass, it's a bad practice.
So anyone has some idea how to acomplish this? Maybe there's a design pattern that fits to this situation?
You cannot move all the different static variables from derived classes into the base class, because static variables are one-per-class; you want your variables to be one-per-subclass, which is not allowed.
You could work around this issue by defining a registry of subclasses in your base class, and store the int for each subclass there. However, this would add a lot more complexity, and it is not clear how you would differentiate between subclasses in the superclass.
Your current solution appears optimal.
Don't use a static field for this - that's not the way to go, because static fields of a subclass do not "override" those of a super class.
Instead, because the values are constant for a given class, use a final instance field:
public class Base {
protected final int variable;
public Base() {
this(5);
}
protected Base(int v) {
variable = v;
}
}
public class Sub1 extends Base {
private static int v = 7;
public Sub1() {
super(v);
}
}
Now the variable is fixed and accessible to all instances.
You can certainly move variable into the base class, but it cannot be static. Alternatively, you can make static getters which you override in each subclass. Here is an example of both:
public class base {
protected int variable;
protected static int getVariable() {
return -1;
}
}
public class Sub1 extends base {
public Base() {
variable = 0;
}
protected static int getVariable() {
return 0;
}
}
public class Sub2 extends base {
public Sub2() {
variable = 1;
}
protected static int getVariable() {
return 1;
}
}
As a design principle, it is somewhat rare (in my opinion) that you genuinely want static methods. Usually you will have some instance of the class around that you are working with. If you want a whole bunch of objects to share some common behavior which you configure at runtime, you might want to check out the flyweight pattern.
Let's say I have a
public abstract class Super{
protected static int BASE = 1;
public int foo(){
//do some computation with BASE, e.g:
return BASE + 1;
}
}
and a couple static classes inheriting from it:
public static abstract class Sub extends Super{
//this class should keep BASE at 1
}
static class SubSub1 extends Sub{
//this class should change BASE to 0
static{
SubSub1.BASE = 0;
}
}
static class SubSub2 extends Sub{
//this class should in effect have a BASE of 1
}
And yes, this fails.
The problem obviously is that since SubSub1 is a static class, if it is to change BASE, Super.BASE must be static.
But if Super.BASE is static, then SubSub1.Base IS Super.BASE.
How do I best achieve what I'm trying to do? (No, I can't make the classes non-static, and assume there are a lot of subclasses.)
I'd appreciate the help.
There are no static classes in Java. All Java classes are implicitly static, except nested classes. Interfaces, Annotations and Enums are always static.
The actual issue is a different one: there is no static inheritance in Java. A subclass does not get a copy of the static superclass field, it gets the same field. Whether you reference it as SubSub.BASE or as Super.BASE doesn't matter, it is Super.BASE
Actually, the "abstract protected int getBase()" wasn't so bad an idea - I just didn't like that "abstract" part since I would have had to implement that in all non-abstract subclasses.
This, on the other hand, seems to be working and allows me to change BASE only where I need it to change:
public abstract class Super{
protected int BASE = 1;
protected void setBase(){}
public int foo(){
setBase();
return BASE + 1;
}
}
and then:
public static abstract class Sub extends Super{
//this class should keep BASE at 1
}
static class SubSub1 extends Sub{
//this class should change BASE to 0
#Override
protected void setBase(){
BASE = 0;
}
}
static class SubSub2 extends Sub{
//this class should still have a BASE of 1
}
Thanks a lot, guys.
I have an abstract class that should implement a public field, this field is an interface or another abstract classe.
something like this:
public abstract class GenericContainer {
public GenericChild child;
}
public abstract class GenericChild {
public int prop1=1;
}
public abstract class SpecialChild extends GenericChild {
public int prop1=2;
}
Now i have another specialized class Container:
public abstract class SpecialContainer extends GenericContainer {
public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
}
Java allow me to compile this, and i IMAGINE that the field child in SpecialContainer is automatically overloading the field child of the GenericContainer...
The questions are:
Am i right on this? The automatic 'overloading' of child will happen?
And, more important question, if i have another class like this:
public class ExternalClass {
public GenericContainer container=new SpecialContainer();
public int test() {
return container.child.prop1
}
}
test() will return 1 or 2? i mean the GenericContainer container field what prop1 will call, the generic or the special?
And what if the special prop1 was declared as String (yes java allow me to compile also in this case)?
Thanks!
In Java, data members/attributes are not polymorphic. Overloading means that a field will have a different value depending from which class it's accessed. The field in the subclass will hide the field in the super-class, but both exists. The fields are invoked based on reference types, while methods are used of actual object. You can try it yourself.
It's called, variable hiding/shadowing, for more details look on here
It isn't overriding anything, you're just hiding the original field at the current class scope. If you use a variable with the subtype you will still be able to access the original property. Example:
abstract class GenericContainer {
public GenericChild child;
}
abstract class GenericChild {
public int prop1=1 ;
}
class SpecialChild extends GenericChild {
public int prop1=2;
}
class SpecialContainer extends GenericContainer {
public SpecialChild child;
}
public class Main {
public static void main( String ... args ) {
GenericContainer container = new SpecialContainer();
container.child = new SpecialChild();
System.out.println( container.child.prop1 );
SpecialChild child = (SpecialChild) container.child;
System.out.println( child.prop1 );
}
}
This prints 1 and then 2.
From SpecialChild you would also be able to go up one level using super:
class SpecialChild extends GenericChild {
public int prop1=2;
public int getOriginalProp1() {
return super.prop1;
}
}
Regarding
....and i IMAGINE that the field "child" in SpecialContainer is automatically overloading the field 'child' of the GenericContainer...
No. Fields don't get overridden, only methods do.
This is one reason why use of (overridable) getter and setter methods are preferred to direct access to fields. Your fields should almost all be private.
As for your design, there's no need for your SpecialContainer class to have a SpecialChild field, but instead the SpecialChild object should be placed in the GenericChild field.
Why nobody is observing that program will throw NullPointerException.
subclass's field with same name will hide super class's field. There is no overriding with field. Overriding is only possible with methods.
Original Code by Author:
public abstract class GenericContainer {
public GenericChild child;
}
public abstract class GenericChild {
public int prop1=1;
}
public abstract class SpecialChild extend GenericChild {
public int prop1=2;
}
public abstract class SpecialContainer extends GenericContainer {
public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
}
public class ExternalClass {
public GenericContainer container=new SpecialContainer();
public int test() {
return container.child.prop1
}
}
Java allow me to compile this, and i IMAGINE that the field "child" in
SpecialContainer is automatically overloading the field 'child' of the
GenericContainer...
Firstly, Inheritence doesn't apply to variables. Fields(Insatnce variables) are not overridden in your sub-class.they are only visible in your subclass if they are marked with either public, protected or default.
To answer your question it maintains both instances. And depending on how you refer to the container (either through the abstract or the impl) determines which variable you are referring to.
public class Test {
public abstract class Container{
public Generic gen = new Generic();
}
public class ContainerImpl extends Container{
public GenericImpl gen = new GenericImpl();
}
public class Generic{
public int prop = 0;
}
public class GenericImpl extends Generic{
public int prop = 1;
}
public Test(){
Container c = new ContainerImpl();
System.out.println(c.gen.prop); // Outputs "0"
System.out.println(((ContainerImpl)c).gen.prop); // Output "1"
}
public static void main(String[] args) {
new Test();
}
}
The bigger question at hand is, why would you design something like this? I'm assuming you are asking from a theoretical perspective.
My 2 cents, this isn't great OO design. You would be better off making the public variables private and assigning their values through a constructor or property setter. As-is, it will lead to unexpected results in your code.