Reference to Abstract Class

reference to abstract class

A reference to an abstract class is just like a pointer to an abstract class: it needs to reference an object of some non-abstract subclass of the abstract class. You can use a reference like that to call virtual methods on the referenced class using the . syntax, in a way similar to a pointer to an interface in Java.

Is it possible to make a reference to an abstract class method in a class method that doesn't extend it?

For sure, you can reference an abstract class and call its abstract classes, but the object you exactly reference should be an extender of the abstract class.

For example, create a list of different objects, all extending one abstract class.

public abstract class ExAbstract { public abstract void abstractmethod() {...} }
public class ExampleA extends ExAbstract { @Override... }
public class ExampleB extends ExAbstract { @Override... }
...

List<ExAbstract> list = new ArrayList<>();
list.add(new ExampleA());
list.add(new ExampleB());
...

And then, you can call abstract method on it.

for (ExAbstract test : list){
test.abstractmethod();
}

(Or Java 8)

list.forEach(ExAbstract::abstractmethod);

But if object wasn't extending abstact, and it was abstract itself, it would give an error.

EDIT: In your case, with Routine class, you should make a constructor for it, and then make a new object. (I see you have a constructor already...) If you want to use a method without creating an object, use static

In Routine.java:

public Routine(ExampleArg a){
this.a = a;
}

In your Routine call:

Routine r = new Routine(a);
r.start();

How can abstract classes have references but not objects?

When you have an object reference whose type is an abstract class, that reference doesn't mean "I'm referencing an abstract class." Instead, it means "I'm referencing some actual object that's a subclass of that abstract class." This is why you can have the reference refer to a SavingsAccount, which is a non-abstract class that subclasses from Account, but you can't have it point to a new Account(), since you can't actually instantiate Account.

Note that the reference itself isn't an actual instance of the abstract class.

Hope this helps!

How to pass reference to abstract class - Java

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
}
}

Child object reference by abstract class type variable or child class type variable?

Super class means you have something in common between some classes, these common things might be properties or method.abstract

class means you have some concept that may need some of its methods implementation done by its subclass.

you may somewhere in your app need vehicle concept regardless of what vehicle it is, paint the vehicle, so, you need just know that passed parameter is a vehicle.

both concepts are right, but it depends on your usage and your designing.

if you need to have only vehicle concept regardless of its subtype, you must use second one, and in this case you need to use method sell() you must use the first one because you made the method available and it's properties came from defining part, not instantiating part.

At the end, study Abstraction in OOP, it may help you more. hope it was helpful :)

Java: Creating Reference Type from Abstract class and interface

Why do you think you are actually instantiating AbstractClass and AbstractInterface?

new Test1("JEFFFFFF") , new Test2("Hey , say something"), new Test4("Helen") , new Test3("Hey , say my name") are all instantiating concrete classes, not abstract ones.

If you refer to AbstractClass[] abstractclass_list = as proof of instantiating abstract classes, that is wrong. Here, you declare an array whose elements are of type AbstractClass, and Test1 and Test2 are (since they extend AbstractClass).

UPDATE

You could have something like this AbstractClass abs = new Test1("hey"); and what it does is it creates a new instance of class Test1, and references that instance from variable abs. abs's concrete type is Test1, but only methods declared in AbstractClass are visible on it. If you want to call methods of Test1, you would have to cast it first.

AbstractClass abs = new Test1("hey");
abs.printname(); // this is ok, and it calls `printname() implemented in Test1
abs.someTest1Method(); // this is NOT ok, someTest1Method() is not visible to abs
((Test1)abs).someTest1Method(); // this is ok, abs is cast to Test1, but would fail if abs was instantiated as 'abs = new Test2("t2")' (would throw ClassCastException)

Safely passing around references to an abstract class in C++

Use std::unique_ptr for unique ownership and std::shared_ptr for shared ownership. This makes passing pointers much safer.

std::unique_ptr cannot be copied, and the pointed-to object is automatically deallocated when the unique_ptr is destroyed (e.g. goes out of scope). Conversely, the std::shared_ptr CAN be copied, and the pointed-to object is only deallocated when all copies of the shared_ptr are destroyed.

If you instrument you code with the above tools and also use std::make_unique (C++14) and std::make_shared (C++11), largely free of manual new and delete and avoid a lot of memory related issues.



Related Topics



Leave a reply



Submit