I have an abstract class with a variable like follows:
public abstract class MyAbstractClass {
int myVariable = 1;
protected abstract void FunctionThatUsesMyVariable();
}
Then when I go to instantiate my class through the following code, myVariable cannot be seen:
MyAbstractClass myClass = new MyAbstractClass() {
#Override
protected void FunctionThatUsesMyVariable() {
// TODO Auto-generated method stub
}
};
What am I doing wrong and how can I achieve what I am trying to achieve?
You are declaring myVariable as having package access and your 2 classes reside in different packages. Thus the variable is not visible to inheriting classes. You can declare it with protected access to be visible or put the 2 classes in the same package.
public abstract class MyAbstractClass {
protected int myVariable = 1;
protected abstract void FunctionThatUsesMyVariable();
}
Seems to work for me:
public class Test {
public static abstract class MyAbstractClass {
int myVariable = 1;
protected abstract void FunctionThatUsesMyVariable();
}
public void test() {
MyAbstractClass myClass = new MyAbstractClass() {
#Override
protected void FunctionThatUsesMyVariable() {
myVariable = 2;
}
};
}
public static void main(String args[]) {
new Test().test();
}
}
I suspect you are declaring the two classes in different packages. If that is what you want then you should make the variable protected (or public if you must). Alternatively - obviously - put them in the same package.
Because you're still subclassing/extending the abstract class and unless you make it protected, the field isn't inherited.
Declare your variable as protected. It's package-private by default.
Related
In this class abstract class object is instantiated by overriding the getNum(), what is the purpose of this?
public abstract class AbstractTest {
public int getNum() {
return 45;
}
public static void main(String[] args) // main function
{
AbstractTest t = new AbstractTest() // From this point didn't understand
{
public int getNum() // function
{
return 22;
}
}; //use of this
System.out.println(t.getNum()); // output
}
}
The instantiation in your main() method is simply an inline class definition of a concrete instance of the abstract class AbstractTest. To be clear, the variable t is an anonymous, non abstract class instance. The following code would achieve the same thing:
public class ConcreteTest extends AbstractTest {
#Override
public int getNum() {
return 22;
}
}
public static void main (String [] args) {
ConcreteTest t = new ConcreteTest();
System.out.println(t.getNum());
}
There are instances in the course of development where it can be cumbersome to have to create a formal class definition. For example, if you only need a single instance of the abstract AbstractTest class, it would be easier to use an inline definition.
We call this 'Anonymous Class': When you need to create and use a class, but do not need to give its name or reused use, you can use an anonymous class. Here is the offical doc. Not only used for abstract class, can also be used for interface and general extensible class.
interface Base {
void print();
}
public static void main(String[] args) {
Base aInterface = new Base() {
#Override
public void print() {
System.out.println("A anonymous implement.");
}
};
Thread aThread = new Thread() {
#Override
public void run() {
super.run();
}
};
}
I have a class in jar of which I want to invoke a method. But that method has parameter of abstract class and that abstract class is inner method of class in jar. AbstractClassA is a HIDDEN class. Here is code:
public class A{
private invokeThisMethod(AbstractClassA object){
}
public abstract class AbstractClassA {
public void update(int remaining){}
}
}
public class myClass{
//using Reflection get object of class A
objectOfClassAusingReflection.inovke("invokeThisMethod", params)
}
Problem here is how do I create concrete implementation of AbstractClassA to pass in invoke method and get update method callbacks ?
Something like this should work:
AbstractClassA a = new AbstractClassA() {
public void update(int remaining) {... do something...}
};
objectOfClassAusingReflection.inovke("invokeThisMethod", a);
You cannot create an instance of abstract class or any interface at runtime.
Instead create an anonymous class for this.
public abstract class A {
public void fun(){....}
public abstract void absFun();
}
public class MyClass {
objectOfClassA = new A(){
public void absFun(){...}
}
}
Or you can first create implementation for that abstract classes for which you will have to create another class extending A
class AWrapper extends A {
public class ImplementationClassA extends AbstractClassA {
// override abstract functions...
}
}
Now you can use this Awrapper class
AWrapper wrapperObj = new AWrapper();
A obj = wrapperObj; // just to make it clear that A can hold wrapperObj as it is implementation of it.
A.AbstractClassA absObj = wrapperObj.new ImplementationClassA();
...
objectOfClassAusingReflection.inovke("invokeThisMethod", params)
Below code should work--
Here, i used anonymus classes for both outer and inner class and then with the help of getdeclatedMethod called your update method.
"TestAbs" is your jar class--
public abstract class TestAbs {
private void invokeThisMethod(AbstractClassA object) {
}
public abstract class AbstractClassA {
public void update(int remaining) {
}
}
}
Then calling your jar class from "TestAbs1" like below--
public class TestAbs1 {
public static void main(String[] args) {
TestAbs.AbstractClassA abs = new TestAbs() {
AbstractClassA a = new AbstractClassA() {
public void update(int remaining) {
System.out.println("Inside update method : " + remaining);
}
};
}.a;
try {
int i = 1;
Class<?> class1 = Class.forName("app.test.mytest.TestAbs$AbstractClassA"); -- (*Getting instance of inner class*)
System.out.println(class1.getDeclaredMethod("update", int.class));
class1.getDeclaredMethod("update", int.class).invoke(abs, i);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The output i got is --
public void app.test.mytest.TestAbs$AbstractClassA.update(int)
Inside update method : 1
Answer to your Comment:-
What I understood from your comment is that, you wanted to call method from abstractClass which is hidden in outerclass.
As per my understanding, there is one way like below--
public abstract class TestAbs {
private void invokeThisMethod(AbstractClassA object) {
}
private abstract class AbstractClassA { --- your hidden class
public void update(int remaining) {
}
}
public class ImplementedClass extends AbstractClassA{ -- use implemented class here
....
...
}
}
And after that, use your ImplementedClass the same way mentioned above.
You can find reference example for private inner class here from java docs.
Note: In your question context, since your inner class and outer class is in jar, so I think it is difficult for you add implementation class in your jar.
In case, you find any alternatives, please let all knows about this;
thanks.
I have these 2 classes
class A {
public void foo1() {
...;
foo2();
...;
}
protected abstract foo2();
}
class B extends A {
public foo2() {
......
}
I need foo2 to be static so I can do B.foo2() but I also want the functionality in class A to remain.n
Any suggestions?
}
You can't override static methods or implement abstract methods as static.
Static methods are defined on a class definition, not on a class instance. Abstract methods are defined on a class instance.
What you said doesn't make sense in fact.
Although I don't quite get why you need to do it, there is a workaround:
class B {
#Override
public void foo() {
fooUtil();
}
public static void fooUtil() {
// your impl here
}
}
Then you can do B.fooUtil() instead, and using its behavior to override A.foo().
I'm new to Java and I have a very basic question.
I have 2 Parent Class under the same package. Animal Abstract Class and the Machine Class.
Now, the Animal Abstract Class has a protected method. I'm aware that protected methods are accessible if the classes are under the same package.
I can access that protected method in my Machine Class, and the question is.. Is it possible to override that protected method? Without extending the Animal Class.
protected - Can be overridden by subclasses, whether they are in the same package or not
default (no access modifier) - can only be accessed or overridden if both the classes are in the same package
You can only override methods through extension.
You can override a protected method with an anonymous subclass, if you like. E.g.
public class Animal {
protected String getSound() {
return "(Silence)";
}
public void speak() {
System.out.println(getSound());
}
}
In another class:
public static void main(String ... args) {
Animal dog = new Animal() {
#Override
protected String getSound() {
return "Woof!";
}
}
dog.speak();
}
Will output:
Woof!
No , Overriding means inherit the behavior from parent class and that is not possible without extending the class.
public class PClass
{
protected boolean methodA()
{
return true;
}
}
public class CClass extends PClass
{
protected boolean methodA()
{
return false;
}
}
Run the code below to test it
public static void main(String[] args)
{
PClass pc = new CClass();
System.out.println(pc.methodA());
}
O/p=false
here we are overriding the behavior of methodA
In order to override a method you have to extend that class. That's what overriding means: having a method with the same signature as the super-class.
Overriding by definition says..
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
So AFAIK if you don't extend the super class there is no way to override the method.
From the Sun docs
Normally, you would put code to
initialize an instance variable in a
constructor.There are two
alternatives to using a constructor to
initialize instance variables:
initialize blocks and final methods.
I could understand the use of initialize blocks. Can anyone please explain the use of final methods for instance var initialization? A non-final public setter can do this job. Why not just use them ?
The advantage is already described in the very same Sun tutorial you linked to:
A final method cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance.
This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems. Joshua Bloch describes this in more detail in Effective Java(item 17 Design and document for inheritance).
The reason a non-final method is dangerous in initialization is because the instance initialization of the superclass executes before the sub class is initialized. Therefore if the non-final method is overriden in the sub class and is executed during the initialization of the superclass it may be accessing uninitialized fields of the subclass giving erroneous results.
The general rule is(quoting from Effective Java): Constructors must not invoke overridable methods, directly or indirectly.
It's explained on the same page of the referenced tutorial. The reason is that a non-final method can be overriden by up subclass. Here's an example:
class Whatever {
private List<String> myVar = initializeInstanceVariable();
protected List<String> initializeInstanceVariable() {
return new ArrayList<String>();
}
}
class Whoever extends Whatever {
#Override
protected List<String> initializeInstanceVariable() {
return Collections.unmodifiableList(super.initializeInstanceVariable());
}
}
So if you create Whoever, myVar will become unmodifiable ;-)
Other examples
From alykhantejani.github.io
I made it compilable and simplified it.
Duck.java
public class Duck {
String sound = "quack";
protected String speech;
public Duck() {
initSpeech();
}
protected void initSpeech() {
speech = "sound = " + sound;
}
public void speak() {
System.out.println(speech);
}
protected String getSound() {
return sound;
}
}
SqueakyDuck
public class SqueakyDuck extends Duck {
String squeakSound = "squeak";
public SqueakyDuck() {
super();
}
#Override
protected void initSpeech() {
speech = "sound = " + squeakSound;
}
#Override
protected String getSound() {
return squeakSound;
}
}
Main.java
public class Main {
public static void main(String[] args){
Duck squeaky = new SqueakyDuck();
squeaky.speak();
System.out.println(squeaky.getSound());
}
}
Output:
sound = null
squeak
My example
Superclass.java
public class Superclass {
protected int x = m();
protected int m() {
return 8;
}
}
Subclass.java
public class Subclass extends Superclass {
private int y = 7;
#Override
protected int m() {
return y;
}
}
Main.java
public class Main {
public static void main(String[] args) {
Superclass s = new Subclass();
System.out.println(s.x);
}
}
Output:
0
Order of execution:
main
m from Subclass (y is not initialized in this moment and 0 is the default value for int)
constructor Superclass
constructor Subclass