Abstract class implements Interface - java

Why an abstract class that implements an interface has not the necessity to implements interface's methods while instead a class has the necessity to implements all the methods?

Since an abstract class leaves the implementation of one or more of it's methods to it's extending classes, it can do the same with the method contracts it inherits through implementing additional interfaces.

It is not necessary that class has to implements all the methods of an implemented interface. If class don't implement all the methods of an interface it can be declared as abstract class.

Abstract class, by definition, is a class that can have unimplemented methods. If an abstract class implements an interface, it is not bound to implement any of the the inherited methods. That doesn't mean you can't implement any of them. :D

Ultimately we have to create a concrete class. Only then we are going to create an instance and use it. Moreover after implementing an interface an abstract class won't become a concrete class but if a concrete class leaves a method unimplemented it must become an abstract class.

Related

Why we are declaring the class as abstract class it has only concrete methods in it?

Abstract class means, it has both abtract methods and concrete methods but even if it has only concrete methods, it is just look like a normal methods only right.
And why we are declaring the class as the abstract without any abtract methods?
On some cases, you do want to have some shared logic/fields/methods between several classes, but you do not want the base class to be instanciated by itself, only the extending classeses.
For such use-cases, abstract class, even without any abstract methods, can do the trick
It could be a base class for implementing some interfaces: all methods are concrete, but not all methods of interfaces are implemented.

must either be declared abstract or implement abstract method

I get the following message when trying to implement a class:
public class MyLocationListener implements BDLocationListener
This sentence is wrong. The hint is:Class 'MyLocationListener' must either be declared abstract or implement abstract method 'onConnectHotSpotMessage(String, int)' in 'BDLocationListener
I use Android Studio.
You are declaring that you're going to "implement BDLocationListener" which is an abstract class.
When you implement an abstract class, you must provide a method for every unimplemented abstract method within the abstract class. In this case, onConnectHotSpotMessage(String, int) is one of the abstract methods in that class.
You must either provide an implementation of this method in your MyLocationListener class or declare your MyLocationListener as an abstract class. If you declare MyLocationListener as an abstract class, you will not be able to instantiate it, so it is more likely that you want to implement the method.
Your BDLocationListener class has an Abstract method that needs to be implemented in your MyLocationlistener, it's like a contract, if a class wants to implement an abstract class it has to implement its abstract methods or to be abstract as well.
You can find more information about Abstract class here

How to create abstract class object which implements interface ( JAVA )

I have an interface
public interface I{
Status getStatus();
}
then I have an abstract class implementing that interface
public abstract class C implements I{
public Status getStatus() {
return status;
}
}
I want to access the getstatus() method from another class, I have tried
C status = new C();
but I am getting error "Cannot instantiate the type C"
Any help will be highly appreciated..!!
thanx.
You can not create object for an abstract class
Write a concrete class which extends your abstract class and use that class object to call the method
class test extends c{
..........
}
c obj1= new test();
obj1.getStatus();
According to docs
A class type should be declared abstract only if the intent is that subclasses
can be created to complete the implementation. If the intent is simply to prevent
instantiation of a class, the proper way to express this is to declare a
constructor of no arguments, make it private, never invoke it, and declare no
other constructors.
Abstract class can have not abstract methods but it must have a valid use case(like calling super from subclass). You cannot instantiate(create objects of abstract class).
So either remove abstract keyword or create another class that extends your abstract class.
Ans just for the record when an abstract class implements an interface you need not implement the interface methods in abstract class(though you can if your design dictates so). But in case you don't implement interface methods in the abstract class implementing it you need to implement the same in first concrete subclass of your abstract class. Also if you do implement you interface methods in abstract class then no need to implement them again in the concrete subclass of the abstract class. You can always override it though.
You can not instantiate abstract class
Assuming that your class must be abstract.
You cannot instantiate an abstract class.
What you can do is,Let any child to extends that class and create object for that.
Abstract Methods and Classes
If you really abstract methods in class C, then I recommend simply using a regular class. You can still extends that calls as well.

can we use extends in place of implement to use interface

I am trying to use extends keyword in place of implement to use interface is it possible in java.
Interface myinterface
{
//methods
}
public class myclass extends myinterface
{
//methods
}
Tell me the purpose of these two words extends and implements. why class is not use implement keyword to inherits the class from other class
Think about the two words and what they are telling you.
Implements - means to put something into effect. An interface is regularly defined as a contract of what methods a class must have, or implement. Essentially you are putting that contract into effect.
Extends - means to make longer. By extending the class you are basically making it longer by also including all the methods of the extended class.
Two different words that are giving you, by definition, two different abilities within your code.
Interface cannot be extended but rather implemented.
Interfaces can contain only constants, method signatures, and nested types. That is they only represent an abstraction of your model or can simply contain a list of constants.
Interfaces support inheritance. You can have for instance :
public interface InterfaceA extends InterfaceB
If you really want to extend from a class and have some abstract methods you can use an abstract class as :
public abstract class AbstractA {
public abstract void myAbstractMethod;
}
public class A extends AbstractA {
#Override
public abstract void myAbstractMethod {
// your code
}
}
No, you have to use implements with interfaces.
You can however make an abstract class if you absolutely need to use extend.
Classes cannot extend an Interface. They can only implement them. Only an Interface can extend another Interface just like only a Class can extend another Class.
Tell me the purpose of these two words extends and implements.
When a class extends it inherits attributes and behaviour i.e. methods from the class it extends from. A class can only extend from one class since multiple inheritance isn't supported in Java.
When a class implements it provides behaviour i.e. implementation for the methods defined as stubs (just the signature without code) in the Interface it implements. A class can however implement multiple interfaces.
When an Interface extends another Interface its simply adding more methods to the list of methods that a Class implementing it needs to provide implementation for.
As others, most succinctly #Stefan Beike, have said: no, you can't use extends when you mean implements. What you can do, if desired, is to add an in-between abstract class which implements your interface, and then extend that class. Sometimes this is done with empty implementations of the interface's methods, and then you only need to override the methods of interest in your child class. But it can be a purely abstract class if all you want is to use extends where implements would otherwise be called for.
Extends - is used by a class for extending some features of another class, so that same method or fields can be reused. Basic example can be :
class Animal
{
private String name;
public void setName(String name)
{
this.name = name;
}
public int getLegs()
{
return 2;
}
}
class Elephant extends Animal
{
public int getLegs()
{
return 4;
}
}
Now, setter is reused and extends doesn't mandate it to be overriden, but as per requirement any method can be overriden also, as getter in our case.
Implements - A class can implement an interface. This helps in achieving abstraction. any method in interface needs to be implemented by any class that is implementing the interface. It is mandatory, until or unless class is abstract, in which case any other concrete class should implement the unimplemented methods.
So, a class can extends other class for reusing functionality, and a class can implement an interface to enforce some functionality that a class must provide by itself.
Now, why interface extends interface, I am also not sure, may be its because sub interface will extend the methods of super interface and it will enforce implementation of methods in super interface on class that is implementing the sub interface. As super interface does not enforce implementation on sub interface, so implements can not be used.
I hope I am clear.
class extends class (Correct)
class extends interface (Incorrect) => class implements interface (Correct)
interface extends interface (Correct)
interface extends class (Incorrect) (Never possible)

Java: Force implementation of interface to extend abstract class

I have several classes implements interface MyInterface and I want all of them to extend abstract class MyAbstractClass. What is the best way to do that?
Is there any better way than create another abstract class extending MyAbstractClass and implementing MyInterface?
(I swear I haven't found any question like this before I posted)
The cleanest solution would be to define such a requirement in the JavaDoc of the interface. In the JavaDoc it should then state something like "to use this interface you need to extend from MyAbstractClass or provide your own implementation". This way the programmer is responsible for the implementation. Remember that this is a sociological problem which you try to solve technicality.
Another 'solution' would be to drop the interface and use an abstract class. Implementing the interface in the abstract class wouldn't make sense here.
You could define MyAbstractClass to implement MyInterface, and then make all of your other classes extend MyAbstractClass:
public abstract class MyAbstractClass implements MyInterface {
...
}
public class OneOfSeveral extends MyAbstractClass {
...
}
You can't force all your concrete classes to extend MyAbstractClass in any other way than actually changing their definitions from
class A implements MyInterface
to
class A extends MyAbstractClass
and of course
abstract class MyAbstractClass implements MyInterface
You don't need another abstract class as you write, though.
Edit: Regarding your comment below: "I want the other way that every implementation of MyInterface extends MyAbstractClass" - you cannot enforce that sensibly. You can define another interface that MyInterface extends and call that MyAbstractClassInterface if you want but you still won't be able to enforce your existing classes extend an abstract class implementing this latter interface, although they will of course have to actually implement the methods defined in this interface...
It sounds to me that you should drop the interface and replace it with the abstract class.
Cheers,

Categories

Resources