must either be declared abstract or implement abstract method - java

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

Related

Can I define an abstract method without the use of abstract keyword in Java?

I know we can define abstract methods inside abstract class only. I also know we can't instantiate abstract class on its own, it must be inherited (extended). And I know that abstract method forces subclasses to provide an implementation for that method.
My question is only if I can define that abstract method without the "abstract" keyword? Something like:
public abstract Animal
{
public void makeNoise();
}
You can create an interface which is a set of abstract methods without using abstract keyword. If you create an abstract class, then every abstract method should have the abstract keyword.
Check the documentation for more info.
My question is only if I can define that abstract method without the
"abstract" keyword?
You cannot define abstract methods that way.
An abstract class can contain the implementation of a method. Because that, you have to mark which method is abstract and which is not.
If you create Animal class
public abstract class Animal {
public void makeNoise();
}
Eclipse will suggest you either add body or change the method to abstract.
My question is only if I can define that abstract method without the "abstract" keyword?
No you can't on classes. That's only possible on interfaces.

Why mark class abstract when there is no abstract method

I am little confused about abstract class in java. I know that whenever there is an abstract method in the class compiler force developer to mark class abstract. But even we don't have any abstract method in the class we still mark the whole class as abstract. I am not getting the point why we can do this. what is the purpose to allow developer to mark class abstract when there is no abstract method. One can say that reason is that we don't want to create instance of that class. If that the reason then marking constructor of the class private is more suitable rather than marking class abstract.
There is a very useful reason for having an abstract class without abstract methods: Providing default implementations for overridable methods.
There are several perfect examples in the JDK itself. Look - for example - at a WindowAdapter. It implements the WindowListener interface (among others), but provides empty not-doing-anything method implementations. In most cases you want to register a window listener that only overrides one or two of the interface methods. Then your own class simply extends WindowAdapter instead of implementing WindowListener.
Note, that with Java 8 default methods in interfaces this reason does not hold anymore, and in fact abstract classes without abstract methods do not make sense anymore.
I think it's to allow subclasses to be created but not the main class.
I guess your class has method stubs in it, otherwise tyere would be no reason not to instantiate it. It is generally better to use abstract methods for this.
For restricting the class to be instantiated.. Example HttpServlet class.. it is defined abstract but has no abstract methods.. We can use these methods in the subclasses but creating the class httpservlet itself is useless.. thats the reason i think..
HTH!
As stated, this can be to prevent instantiation. I strongly prefer private or protected constructors over this as I feel they communicate the intent more clearly.
Also, in a class hierarchy, if class A is abstract and contains an abstract method, that method does not need to be defined in a class B which extends class A. In this case, class B is marked as abstract and has no abstract members.
To prevent instantiation of a class and use it as a base class. For example, HttpServlet class, an example of template method design pattern where each method already has a behaviour defined. The child class is free to override one or more of them instead of all of them.
One can say that reason is that we don't want to create instance of
that class. If that the reason then marking constructor of the class
private is more suitable rather than marking class abstract.
No it is not at all suitable
This below example will clear your doubts , If you use private constructor , Not only your Object creation is blocked but also you can not even create a subclass of the Parent class
class ParentClass{
private ParentClass(){
}
}
class Subclass extends ParentClass{
static{
System.out.println("Hello");
}
}
You will get compile time error saying
error: ParentClass() has private access in ParentClass
But Marking a class as abstract will block Object creation but will not block Inheritence in java
Update
As you asked in comments that you can make it protected but then your class can be easily instantiated , because protected member can be accessed from the same class as well as in SubClass in same package as well as in a sub class in another package .

Is it necessary to declare abstract method in java if class is abstract

If we declare method as abstract then class by default becomes abstract .Then can we apply this vice versa .
If we declare method as abstract then class by default becomes abstract .
This statement is wrong. If you try to declare an abstract method in a class that is not declared abstract, you will get a compilation error.
If you declare a class as abstract, then you've declared the class as abstract. It need not have any abstract methods.
It can not be applied vice-versa, as an abstract class also can have concrete methods.
Complementing the Sotirios answer, abstract class not need abstract methods and if you want an abstract method, you will need turn this method abstract, declaring it.
if a class includes abstract methods, then the class itself must be declared abstract.

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.

Abstract class implements Interface

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.

Categories

Resources