How to create abstract class object which implements interface ( JAVA ) - 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.

Related

What is the Concrete class in java

According to this document, and many similar documents, a concrete class is described as:
A concrete class in Java is any such class which has implementation of all of its inherited members either from interface or abstract class
And can used like this:
public abstract class A {
public abstract void methodA();
}
interface B {
public void printB();
}
public class C extends A implements B {
public void methodA() {
System.out.print("I am abstract implementation");
}
public void printB() {
System.out.print("I am interface implementation");
}
}
In the above example class C is a concrete class.
Is this the only way to create a concrete class. Can you give me more info about concrete class?
A concrete class is a class that has an implementation for all of its methods that were inherited from abstract or implemented via interfaces. It also does not define any abstract methods of its own. This means that an instance of the class can be created/allocated with the new keyword without having to implement any methods first. Therefore it can be inferred that any class that is not an abstract class or interface is a concrete class.
In your code above, C will be a concrete class as it implements all abstract methods inherited from A and implemented from B. Also, it does not define any abstract methods of its own.
The simplest definition of a concrete class is that it's a class that is not abstract.
As per name suggests, concrete means Solid, it means having no any row part or unimplemented things(methods).So we can conclude that concrete classes are those classes that can be instantiated with new key word.
MyClass myClass = new MyClass();
1.concrete class is a class which can never become
an abstract or interface .It can extend or implement or both.
2.The class is said to be concrete if all its methods and variables has defined.
A concrete class in Java is any such class which has implementation of all of its inherited members either from interface or abstract class
In the above program, representing abstract as public class will sometimes show some compile time errors to define that in its own file. As simple, just avoid using public keyword or modifier while using abstract class in your program to avoid some uncertainty. Any method that is invoked using new keyword (object creation) other than abstract and interface classes is called as concrete class.

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

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)

Trouble with implementing abstract super class Java

I'm working on an assignment where I'm trying to implement a class that extends an abstract super class, but by using
public class B extends A{}
However, that gives me the error that type B must implement the abstract method A.act, is there another way in which I can do this without using extends?
Using:
public abstract class B extends A{}
Gives me a different error on another part of the assignment that should be correct.
What should I do? Thanks for the help.
In order for B to instantiated by the JVM, it must not be abstract. A abstract class is an idea or suggest of how a class might work, with some (or all) of the functionality needing to implemented by those classes that extended it.
For B to be instantiated, it must implement all the abstract methods of A.
To solve your problem, you must provide an implementation of the methods that A has marked as abstract...
public abstract class A {
public abstract void implementMe();
}
public class B extends A {
public void implementMe() {
// Your implementation
}
}
The reason for much of this is it allows you to pass B to methods and classes that use A and also change the way in which A works. This is part of polymorphism, where one instance of object can act as its parent (B acting like it's A)
I don't know if it will help, but you could have a read of Abstract classes
and methods
don't declare method act in abstract class A as abstract method if you do not want to provide implemenation of act in B. But that may not be a right choice depending upon what you are trying to achieve. In second case, when abstract class extends another abstract class, use "implement" instead of "extends". hope it helps

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