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.
Related
Suppose there is class A which is parent class and class B that extends A.
Now I should not allow to extend class A to class C (class C extends A), or any other class.
How to do that?
That's why Java 17 has sealed classes. This feature was added as a preview feature in Java 15.
Sealed classes and interfaces restrict which other classes or
interfaces may extend or implement them.
In your case you can do the following:-
public sealed class A permits B {
}
public class B extends A{ //No error
}
public class C extends A{ //error because class C is not permitted
}
The Goals of Sealed classes in java are :-
Allow the author of a class or interface to control which code is responsible for implementing it.
Provide a more declarative way than access modifiers to restrict the use of a superclass.
Support future directions in pattern matching by providing a foundation for the exhaustive analysis of patterns.
IF you want a class not extendable, then you can use final.
public final class A{ //Cannot be extended by other classes.
}
Helpful links:
https://docs.oracle.com/en/java/javase/15/language/sealed-classes-and-interfaces.html
http://openjdk.java.net/jeps/409
https://www.baeldung.com/java-sealed-classes-interfaces
What are sealed classes in Java 17
If you are using an older version of Java, the natural solution is simply to make class A package-private. This prevents classes outside of the package from subclassing A directly.
If A itself does need to be exposed publicly, there is still a solution: declare another class PublicA which is public and final, where PublicA is a subclass of A with no additional or overridden members. Then classes in other packages can use PublicA instead of A, but they cannot subclass it.
The only other practical difference between PublicA and A is that the class B is a subclass of A but not a subclass of PublicA; so for example obj instanceof PublicA won't work to detect instances of B or other public subclasses of A. If an instanceof check like this might be required in other packages, then either a public interface can be exposed which A implements, or PublicA can expose a static method which returns arg instanceof A from inside the package, where it is allowed.
From Java 15 onward such use-case can be implemented by using sealed modifier and permits clause
The restriction of a class or interface to become a parent for a specific class or interface.
Following are two use case one for class and another for interface.
1. Using Class: Suppose we have an class ClazzA, we want to restrict this class to extend only by class ClazzB. That is no any other class can extends ClazzA except ClazzB.
public abstract sealed class ClazzA permits ClazzB {
final String str;
public ClazzA(String str) {
this.str = str;
}
public void method() {
// TODO
}
}
2. Using Interface: Suppose we have an interface InterfaceA, we want to restrict this interface to extend only by interface InterfaceB.
That is no any other interface can extends except InterfaceB
public sealed interface InterfaceA permits InterfaceB {
int method1();
default int defaultMethod() {
return 0;
}
}
Not sure why I would need to, but would it be possible in Java to have an abstract super with a concrete subclass that has an abstract sub of its own? Presuming that abstract would eventually have concrete classes under it.
Yes, you can have both concrete class extending abstract class and vice versa - abstract class extending concrete class.
public abstract class A {
}
public class B extends A {
}
public abstract class C extends B {
}
No that is a really bad practice. Event if Java allows it, it doesn't have any sense from the point of view of UML diagrams.
A class is abstract if it doesn't implement all the behavior expected. Then in the concrete sub-class, it is concrete because it implements all the behavior needed. Then, having an abstract sub-sub-class doesn't make any sense. It is a specialization of a concrete class, meaning it has all the protocol needed for that object.
In other terms, a specialization of a concrete concept can't be abstract.
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.
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)
What I mean is:
interface B {...}
interface A extends B {...} // allowed
interface A implements B {...} // not allowed
I googled it and I found this:
implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible.
However, interface is an 100% abstract class, and an abstract class can implement interfaces (100% abstract class) without implement its methods. What is the problem when it is defining as "interface" ?
In details,
interface A {
void methodA();
}
abstract class B implements A {} // we may not implement methodA() but allowed
class C extends B {
void methodA(){}
}
interface B implements A {} // not allowed.
//however, interface B = %100 abstract class B
implements means implementation, when interface is meant to declare just to provide interface not for implementation.
A 100% abstract class is functionally equivalent to an interface but it can also have implementation if you wish (in this case it won't remain 100% abstract), so from the JVM's perspective they are different things.
Also the member variable in a 100% abstract class can have any access qualifier, where in an interface they are implicitly public static final.
implements means a behaviour will be defined for abstract methods (except for abstract classes obviously), you define the implementation.
extends means that a behaviour is inherited.
With interfaces it is possible to say that one interface should have that the same behaviour as another, there is not even an actual implementation. That's why it makes more sense for an interface to extends another interface instead of implementing it.
On a side note, remember that even if an abstract class can define abstract methods (the sane way an interface does), it is still a class and still has to be inherited (extended) and not implemented.
Conceptually there are the two "domains" classes and interfaces. Inside these domains you are always extending, only a class implements an interface, which is kind of "crossing the border". So basically "extends" for interfaces mirrors the behavior for classes. At least I think this is the logic behind. It seems than not everybody agrees with this kind of logic (I find it a little bit contrived myself), and in fact there is no technical reason to have two different keywords at all.
However, interface is 100% abstract class and abstract class can
implements interface(100% abstract class) without implement its
methods. What is the problem when it is defining as "interface" ?
This is simply a matter of convention. The writers of the java language decided that "extends" is the best way to describe this relationship, so that's what we all use.
In general, even though an interface is "a 100% abstract class," we don't think about them that way. We usually think about interfaces as a promise to implement certain key methods rather than a class to derive from. And so we tend to use different language for interfaces than for classes.
As others state, there are good reasons for choosing "extends" over "implements."
Hope this will help you a little what I have learned in oops (core java) during my college.
Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.
Here is an example below, this is my understanding and what I have learnt in oops.
interface ParentInterface{
void myMethod();
}
interface SubInterface extends ParentInterface{
void anotherMethod();
}
and keep one thing in a mind one interface can only extend another interface and if you want to define it's function on some class then only a interface in implemented eg below
public interface Dog
{
public boolean Barks();
public boolean isGoldenRetriever();
}
Now, if a class were to implement this interface, this is what it would look like:
public class SomeClass implements Dog
{
public boolean Barks{
// method definition here
}
public boolean isGoldenRetriever{
// method definition here
}
}
and if a abstract class has some abstract function define and declare and you want to define those function or you can say implement those function then you suppose to extends that class because abstract class can only be extended. here is example below.
public abstract class MyAbstractClass {
public abstract void abstractMethod();
}
Here is an example subclass of MyAbstractClass:
public class MySubClass extends MyAbstractClass {
public void abstractMethod() {
System.out.println("My method implementation");
}
}
Interface is like an abstraction that is not providing any functionality. Hence It does not 'implement' but extend the other abstractions or interfaces.
Interface is the class that contains an abstract method that cannot create any object.Since Interface cannot create the object and its not a pure class, Its no worth implementing it.