Could we call super.someMethod() in derived class from an abstract class? - java

Could we call super.someMethod() in derived class from an abstract class?
for in stance:
abstarct class TestBase{
void nonabstractMethod(){
....
}
}
Then derived class:
class Child extend TestBase{
void callFunction(){
}
void nonabstractMethos(){
super.nonabstractMethos();
}
}
I assume this can be done. But if we have an abstract method then it cannot be called because of no implementation, am i correct?

The short answer: yes.
You can always call a public or protected super method. Like any (instance) method in java, it will be handled polimorphicly, and a concrete implementation will be called, either of the super's class or from the derived class if it overrides it.

Yes you are correct. If you are extending an abstract class having abstract method, you can't call super.thatMethod();
Consider the following example
public class RSAService {
protected void doRSA(){}
}
class MyService extends RSAService{
public void myService(){
super.doRSA(); //Works fine
}
}
This will work as the doRSA() is accessible from the MyService. Same for public but not for private
But
public abstract class RSAService {
protected abstract void doRSA();
}
class MyServe extends RSAService{
public void myService(){
super.doRSA(); //This won't work
}
#Override
protected void doRSA() {
}
}
Now consider this case, where you can call the super.superClassMethod() from your subclass
public abstract class RSAService {
protected void doRSA(){}
}
class MyService extends RSAService{
public void myService(){
}
#Override
protected void doRSA() {
super.doRSA();
}
}
So if you are overriding a super class method you can call the method using super. Consider this Java Specification link for more clarification

Your example will work if both classes are in the same package.
If that is not the case, then you should declare the method protected or public, something like:
abstract class TestBase{
protected void nonabstractMethod(){
....
}
}
If your method is abstract, then you can't call it, for example:
abstract protected void abstractMethod();

Related

java interface method removing from subclass

Anyone provide suggestion for below mentioned:
in java8 consider an interface having two methods (eg interface1 ,interface 2)
implementing those to many subclass later i want to remove one method interface1 from one of my subclass without affecting other is any possible solution is there?
If your subclass declares that it implements this interface, then you have no choice but to provide implementations for all methods, or declare the class abstract. If you want a concrete class which however does not functionally implement all methods in the interface, then here is one option:
public interface YourInterface {
void method1();
void method2();
}
public class YourSubClass implements YourInterface {
#Override
public void method1() {
// actually do something
}
#Override
public void method2() {
throw new UnsupportedOperationException("method2() is not supported here.");
}
}
Here while we do implement all methods, we throw a runtime exception should a caller try to access method2().
You can do this by providing a default method implementation for the interface1 method in the interface itself.
interface Interface {
default void interface1() {
System.out.println("interface1");
}
void interface2();
}
class Clazz implements Interface {
#Override
public void interface2() {
System.out.println("interface2");
}
}
Depends how you define 'remove one method'.
If you have interface
interface Interface{
void interface1();
void interface2();
}
And for example two subclasses that extend it:
class Class1 implementes Interface {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
class Class2 implementes Interface {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
Then there are two scenarios:
You don't want to implement for example interface1() method in Class2:
You don't want to have interface1() method in Class2
In case of 1. as Robby Cornelissen mentioned, you can simply provide default implementation in Interface:
default void interface1() { /*do default thing*/ }
In case of 2. you need to remove the interface1() method from the Interface.
You can do that by simple moving definition of method interface1() to Class1 (and any other sublass that needs to have it). but that is not really generic approach.
Best is to extract for example Interface1 with method interface1() and use that interface in classes that need to have that method. You will end up with this situation:
interface Interface{
void interface2();
}
interface Interface1{
void interface2();
}
And for example two subclasses that extend it:
class Class1 implementes Interface, Interface1 {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
class Class2 implementes Interface {
#Override
public void interface2(){ ... }
}

implement an abstract method in derived class as static

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().

Prevent abstract implementation from overriding certain method

I have an interface called Worker which I want to expose so that the end-user can simply call:
Worker w = WorkerFactory.createInstance();
w.mainBit();
How can I prevent classes which extend my AbstractWorker class from providing their own implementation of the mainBit method?
This is the structure I have so far:
interface Worker {
void mainBit();
}
class WorkerFactory {
public static Worker createInstance() {
return new WorkerImpl();
}
}
abstract class AbstractWorker implements Worker {
#Override
public void mainBit() {
this.doThing1();
this.doThing2();
}
public abstract void doThing1();
public abstract void doThing2();
}
class WorkerImpl extends AbstractWorker {
#Override
public void doThing1() {
}
#Override
public void doThing2() {
}
#Override
public void mainBit() {
// I don't want classes to override this functionality
}
}
You can do that by making the method final.
Use the final keyword.
public final void mainbit ()
...
Mark the method as final, which prevents overriding:
public final void mainBit()
If you want to always use the AbstractWorker's mainBit, make it final in this class. This way, the subclasses won't override it.
Mark it final inside you abstract class (in Java). No other subclass will be allowed to override it.

Overwriting methods: how to "inject" into the super-method?

Assuming three classes, one being a subclass of the other. Each overwrite the parents' method.
public class BaseClass {
public void doStuff() {
performBaseTasks();
}
}
public class MiddleClass extends BaseClass {
// {BaseClass} Overrides
public void doStuff() {
performMiddleTasks();
super.doStuff();
}
}
public class FinalClass extends MiddleClass {
// {BaseClass} Overrides
public void doStuff() {
performFinalTasks();
super.doStuff();
}
}
When calling new FinalClass().doStuff(), this would lead to a method
invokation order as follows:
performFinalTasks();
performMiddleTasks();
performBaseTasks();
I want to bring the perfomFinalTasks() between performMiddleTasks() and
performBaseTasks(). How can I do this?
performMiddleTasks();
performFinalTasks();
performBaseTasks();
Write a public method in final class doStuffDifferently() and invoke these methods in that order. I am not sure it's possible to do it via any other tricks in the doStuff() method.
One possible way, if you can make the middle class abstract:
public abstract class MiddleClass extends BaseClass {
// {BaseClass} Overrides
public void doStuff() {
performMiddleTasks();
doProxyExec();
super.doStuff();
}
public abstract void doProxyExec();
}
You override the proxy method in your subclass:
public class FinalClass extends MiddleClass {
// {BaseClass} Overrides
public void doStuff() {
super.doStuff();
}
// {MiddleClass} Overrides
public void doProxyExec(
performFinalTasks();
}
}
A not very polymorphic way of method call chaining, but then again the original design is kind of ... odd.

Overriding Protected Methods

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.

Categories