Implementing interface methods in an abstract class - java

I have a concrete class which extends an abstract class, which in turn extends an abstract class. This top class implements an interface. Can I implement the interface method in the top abstract class, so that the concrete sub-class does not have to?
Also, if the interface method takes an Object type as it's parameter, can I have the implementation in the top abstract class take another type (which seems right to me), or would I have to use exactly the same method signature?

Can I implement the interface method in the top abstract class, so that the concrete sub-class does not have to?
Yes
Also, if the interface method takes an Object type as it's parameter, can I have the implementation in the top abstract class take another type (which seems right to me), or would I have to use exactly the same method signature?
It has to be the same signature. However, you can trick with generics, if you want to and if you explain your plan in more detail.

for your fist quetion
Can I implement the interface method in the top abstract class, so that the concrete sub-class does not have to?
you can and you should if its a common feature for all the subclasses.
below is the example for the same
interface X{
void show();
}
abstract class A implements X{
public void show(){
System.out.println("show");
}
}
abstract class B extends A{
}
class C extends B{
}

for your second question
No , signature must be same always.
so below program illustares it
interface X{
void print(Object obj);
}
abstract class A implements X{
#Override
public void print(Object obj) {
System.out.println("print");
}
}
below program wont compile if you change type in subclass
interface X{
void print(Object obj);
}
abstract class A implements X{
#Override
public void print(A obj) {
System.out.println("print");
}
}

Related

Interface extending a class

Let's say that I have an interface, and all classes that implement that interface also extend a certain super class.
public class SuperClass {
public void someMethod() {...}
}
public interface MyInterface {
void someOtherMethod();
}
//many (but not all) sub classes do this
public class SubClass extends SuperClass implements MyInterface {
#Override
public void someOtherMethod() {...}
}
Then if I'm dealing with an object of type MyInterface and I don't know the specific sub class, I have to hold two references to the same object:
MyInterface someObject = ...;
SuperClass someObjectCopy = (SuperClass) someObject; //will never throw ClassCastException
someObjectCopy.someMethod();
someObject.someOtherMethod();
I tried making the interface extend the super class, but it's a compiler error:
public interface MyInterface extends SuperClass {} //compiler error
I also thought of combining the interface and the super class into an abstract class like so:
public abstract class NewSuperClass {
public void someMethod();
public abstract void someOtherMethod();
}
But then i can't have a sub class that doesn't want to implement someOtherMethod().
So is there a way to signify that every class that implements an interface also extends a certain class, or do I have no choice but to carry around two references to the same object?
I think that the only solution you have would be to have a reference to both, but this indicates that you have a design flaw somewhere. The reason I say is because you should think of an interface as something that your implementing classes will always need. For example, a Car and Airplane both need a Drive() interface. A design reconsideration is probably worth your time. However, if you still want to follow that path, you can do the following:
public class ClassA {
public void methodA(){};
}
public abstract class ClassB extends Class A{
public void methodB();
}
After you have the above setup, you can now reference an object that has the two methods by doing the following:
ClassB classB = new ClassB();
classB.methodA();
classB.methodB();
Now you don't actually have to actually use two pointers to the same object.

How does an abstract method differ when it is defined in an abstract class or an interface?

Assume an abstract class has only abstract methods declared in it and the interface has abstract methods.
How does this affect the abstract method m?
abstract class A {
abstract void m();
}
interface A {
abstract void m();
}
Even if the abstract class is a pure abstract, i.e. contains only abstract methods and does not contain state, your subclass extends this class and therefore you have single inheritance only.
However you can implement several interfaces.
Therefore if you want to force your classes to be a subclass of your A and not to be subclass of something else, use abstract class. Otherwise define interface.
Interfaces are also more useful when you don't exactly know if all your entities will need to override a specific method. Consider for instance birds, some can fly and some can't. You don't want to force overriding a fly() method in all Bird subclasses.
public abstract class Bird(){
...
public abstract void eat(); //all birds eat
public abstract void sleep(); //all birds sleep
public abstract void fly(); //is wrong here since not all birds can fly
}
A more correct way would be to define interfaces for specific cases, so in this case
public interface Flyable{
abstract void fly();
}
Then you can define your subclasses as birds that can fly or not based on the fact they implement the Flyable interface or not.
public class Eagle extends Bird implements Flyable{
#override
public void fly(); //needed
-----
}
public class Ostrich extends Bird{
//does't fly
}

Should I implement all the methods present in an abstract class?

Below is the code snippet:
public abstract class MyAbstractClass {
public abstract void a();
public abstract void b();
}
public class Foo extends MyAbstractClass {
public void a() {
System.out.println("hello");
}
public void b(){
System.out.println("bye");
}
}
public class Bar extends MyAbstractClass {
public void a() {
System.out.println("hello");
}
public void delta() {
System.out.println("gamma");
}
}
There are couple of questions that I have:
Q-1 :- Should I implement ALL the methods in abstract class?
Q-2 :- Can the implementing class have its own methods?
When you extend an Interface or an Abstract class you are creating a contract of sorts with that superclass. In the contract you are saying:
"I will implement all unimplemented methods in my superclass"
If you do not, implement all the unimplemented methods, then you are breaking your contract. A way to not break your contract is make your subclass Abstract as well as a way of saying
"I have not implemented all the classes in my contract, I am going to
have my subclasses implement them".
For your class bar right now, you must implement b() or make bar an Abstract class or you are not fulfilling your contract with MyAbstractClass
The basic idea is:
Interface: None of my methods are implemented. A subclass must implement all my methods in order to implement me. (Note: I believe default interfaces have been added to Java 8 which may change this a bit)
Example:
public interface myInterface
{
//My subclasses must implement this to fulfill their contract with me
public void methodA();
//My subclasses must implement this to fulfill their contract with me
public void methodB();
}
Abstract: I may implement some of my methods, but I will also leave methods as abstract so that my subclasses must implement because they can implement those classes to suit their needs better than I can.
Example:
public abstract class myAbstractClass
{
//My subclasses must implement this to fulfill their contract with me
public abstract void methodC();
public void helloWorld()
{
System.out.println("Hello World");
}
}
Abstract classes can also extend interfaces so they can implement some of their methods. But they can also leave some of the methods unimplemented so the subclass can implement them. If you leave an interface method unimplemented, there is not need to declare it abstract, it is already in the contract.
Example:
public abstract class myAbstractClass2 implement myInterface
{
#Override
public void methodA()
{
// this fulfills part of the contract with myInterface.
// my subclasses will not need to implement this unless they want to override
// my implementation.
}
//My subclasses must implement this to fulfill their contract with me
public abstract void methodD();
}
So in essence, an abstract class doesn't have as strict a contract with it's superclass because it can delegate its methods to its subclasses.
Regular Class: (I use regular to mean non-interface, and non-abstract). I must implement all unimplemented methods from all of my superclasses. These classes have a binding contract.
Example:
public class mySubClass extends myAbstractClass2
{
#Override
public void methodB()
{
//must be implemented to fulfill contract with myInterface
}
#Override
public void methodD()
{
//must be implemented to fulfill contract with myAbstractClass2
}
public void myMethod()
{
//This is a method specifically for mySubClass.
}
}
Q-1:- Should I implement all methods in abstract class?
Yes, you must implement all abstract methods.
Q-2 :- Can the implementing class have its own methods?
Yes, you can declare own (more specfic) methods.
You not only should, but have to implement all abstract methods (if the subclass is non-abstract). Otherwise an object of that subclass wouldn't know what to do if that method was called!
The only way to prevent this is if the subclass is also declared abstract, so that it cannot be instantiated in the first place.
You don't have to implement all methods of an abstract class. But you must implement all abstract methods of it.
In fact extending an abstract class has no difference then extending a normal class. It's not like implementing interfaces. Since you're extending you are creating a subclass thus you can add as many methods and attributes as you need.
Ya definately implementing class can define its own method as well and if are not implementing all the methods of your abstract class in the derived class then mark this derived class also as Abstract
but at the end of chain you have to make one concrete class which implements all the method that was not implement in abstract sub-parent
public interface I{
public void m();
}
public abstract class A1 implements I{
//No need to implement m() here - since this is abstract
}
public class B1 extends A1{
public void m(){
//This is required, since A1 did not implement m().
}
}
public abstract class A11 extends A1{
//Again No need to implement m() here - since this is abstract
public abstract void newA11Method()
}
public class B11 extends A11{
//This class needs to implement m() and newA11Method()
}
Yes, the implementing class need only implement the methods labeled as abstract in the abstract class.
Yes you must implement all the methods present in an abstract class. As the purpose of abstract class is purely to create a template for the functions whose implementation is decided by the class implementing them. So if you don't implement them, then you are breaking the concept of abstract class.
To answer your second question, yes you can create any number of your own methods irrespective of the abstract class you are extending.
Yes, When you are implementing an Interface you will have to implement all its methods. That is the purpose of defining an Interface. And yes, you can have your own methods in the class where you implement an Interface.
Yes, when you extends abstract you should give implementation for all abstract methods which are presents in abstract class. Othrewise you should make it implementation class as abtract class.
You must implement all the abstract methods you have in the abstract class. But you can directly use the concrete methods that are implemented.
For more information please refer to the :
https://www.javatpoint.com/abstract-class-in-java

implementing abstract methods/classes in java

Can I implement abstract methods in an abstract base class A in java?
If the answer is yes and there is an implemented abstract method in a base class A and there is a derived class B from A (B is not abstract). Does B still has to implement that base abstract method?
If I understand your question correctly, Yes.
public abstract class TopClass {
public abstract void methodA();
public abstract void methodB();
}
public abstract class ClassA extends TopClass {
#Override
public void methodA() {
// Implementation
}
}
public class ClassB extends ClassA {
#Override
public void methodB() {
// Implementation
}
}
In this example, ClassB will compile. It will use it's own implementation of methodB(), and ClassA's implementation of methodA(). You could also override methodA() in ClassB if desired.
You could have two abstract classes, X and Y, where Y extends X. In that case it could make sense for Y to implement an abstract method of X, while still being abstract. Another non-abstract class Z could extend Y. However, in your example, for A to implement its own abstract methods is a contradiction, the point of making them abstract is so it doesn't provide implementations, it just specifies what the method signatures should look like.
If you implement an abstract method it's not really abstract any more, so no.
Abstract classes can have regular methods. If you want to implement some of the methods of class A and leave rest of the methods abstract, you can do this. However, abstract methods cannot have a body, therefore if you mark a method as abstract, then it has to be implemented by a subclass, you can't implement them in the abstract class. However, you can have an abstract class without abstract methods, then subclass only needs to extend it.
Yes, you can implement abstract methods in a class which is declared as abstract. If a class is declared abstract that does not mean all its method must be abstract.
For a concrete sub class, it is not mandatory to implement the abstract methods that are already implemented by one of their super class.
No. Abstract methods are meant to be defined by the subclass(es). For more information, see Abstract Methods and Classes. However, you can define a method in the base class and have the subclass(es) override it, if required.
Yes, but it can't be abstract any more. Abstract means there is no implementation.
What you can do is:
interface I {
void meth();
}
//and
abstract class A implements I {
public void meth() {
//implementation
}
}
Or:
abstract class A {
public abstract void meth();
}
//and
abstract class B extends A {
public void meth() {
}
}
If A already has an implementation, you can override it in B (if B is concrete), because B inherits that default implementation from A.

Java local classes and interfaces

I was wondering if the next thing is possible for implementation:
Lets say I've got 2 interfaces while each one of them has 1 function header.
For example, iterface1 has function g(...) and interface2 has function f(...)
Now, I make a class and declaring that this class is implementing these 2 interfaces.
In the class I try doing the next thing:
I start implementing function g(...) and in it's implementation I make a local class that implements interface2 and I add to this class the implementation of f(...).
I'm not quite sure what you mean. I am picturing something like this:
interface Interface1
{
public void g();
}
interface Interface2
{
public void f();
}
class MyClass implements Interface1, Interface2
{
#Override
public void g()
{
class InnerClass implements Interface2
{
#Override
public void f()
{
}
}
}
}
Is that what you meant?
In this case, the answer is no. The inner class (InnerClass) works fine, but it doesn't count as an implementation of f for the outer class. You would still need to implement f in MyClass:
MyClass.java:11: MyClass is not abstract and does not override abstract method
f() in Interface2
Yes it is legal. However a class does not implement an interface because an inner class implements it. The class must implement the interface explicitly or declare itself as abstract.
Yes, it's legal. In the example you've given, your class should implement all methods of both interfaces, and your local class should implement all methods of interface2.

Categories