Overriding of Abstract Method in Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Abstract method's property is that it doesn't have body, and so when concrete class extends abstract class, it must implement that abstract method.
So my question is how can an abstract method can be overridden when it doesn't have a body? Isn't it implemented, not overridden?

Abstract class with an abstract method.
The method is also marked with abstract and has no body.
public abstract class A {
public abstract void method();
}
Concretization in the class B that extends abstract class A. Also, the method loses the abstract keyword and gets the body.
public class B extends A {
#Override
public void method() {
System.out.println("Hello");
}
}

public abstract class BaseClass {
public abstract void doSomething();
}
public class ConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hi!");
}
}
public class AnotherConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hello!");
}
}
The implementation of the doSomething method that gets executed will depend on the runtime type of the object you are calling doSomething upon.

Just by providing an implementation for the method , provide it with a body.
public abstract class Base {
public abstract void someMethod(); // no implementation here
}
public class Sub extends Base {
#Override
public void someMethod() {
//It is Overridden
}
}

public abstract class A {
public abstract String getValue();
}
public class B extends A {
#Override
public String getValue(){
return "I'm Overriden";
}
}
It's the same like in any other class.

When you are implementing a superclass method in subclass, it will override. So Even if the superclass method is abstract, if you implement it in subclass, then it will override it.
So,
public class SuperClass{
public abstract void someMethod();
}
public class SubClass{
public void someMethod(){
System.out.println("subclass method");
}
}

Foo.java
public abstract class Foo {
public abstract void doStuff();
}
Bar.java
public class Bar extends Foo {
#Override
public void doStuff() {
// doStuff
}
}

Abstract method can be overridden just like overriding other normal methods, by providing an implementation.

This is the same as any other method override:
public abstract class AbstractClass {
public abstract void abstractMethod();
}
...
public class ConcreteClass {
#Override
public 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(){ ... }
}

Java - class extending abstract class and implementing an interface having same methods

like in topic. Here's an example:
public abstract class Bird{
public abstract void eat();
public abstract void fly();
}
public interface Flyable{
public void fly();
}
public class Test extends Bird implements Flyable{
public void eat(){
System.out.println("I'm eating");
}
// And what's now?
public void fly(){
}
}
And now, there is main question. What happens. Is an error being throwed, or fly is same for interface and abstract class?
Nothing happens. Just implement your logic inside fly() and be happy.
If the methods have the same signature, everything will be fine. It is also okay to have the implementation in the abstract class or to implement a method which is specified in multiple interfaces of the class.
In Java, a method is identified by its name and its parameters. Consequently, the return type of the implemented method must be compatible with all return types of all specified methods with the same identifier. The same applies to the throw clauses. If the return type or throw clauses of the implemented method are incompatible, you will get a compilation error.
This example is not working:
public interface Flyable {
void eat();
void fly();
}
public abstract class Bird {
public int eat() {
return 500;
}
public void fly() throws StallException {
}
}
public class Eagle extends Bird implements Flyable {
}
Eagle.java, line 1: Exception StallException in throws clause of Bird.fly() is not compatible with Flyable.fly()
Eagle.java, line 1: The return types are incompatible for the inherited methods Flyable.eat(), Bird.eat()

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

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

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

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.

Categories