Can yous brand an abstract class/method in conclusion inward Java?

No, you cannot brand an abstract course of pedagogy or method concluding inward Java because abstract in addition to concluding are the mutual exclusive concept. An abstract course of pedagogy is incomplete in addition to tin alone live on instantiated yesteryear extending a concrete course of pedagogy in addition to implementing all abstract methods, piece a final class is considered every bit consummate in addition to cannot live on extended further. This agency when you lot brand an abstract course of pedagogy final, it cannot live on extended thus it cannot live on used in addition to that's why Java compiler throws a compile fourth dimension mistake when you lot travail to brand an abstract class concluding inward Java. In short, an abstract course of pedagogy cannot live on concluding inward Java, using both abstract in addition to final modifier amongst a course of pedagogy is illegal inward Java.

Same is truthful for abstract methods, you lot cannot brand them concluding inward Java. An abstract method must live on overridden to live on useful in addition to called but when you lot brand the abstract method concluding it cannot live on overridden inward Java, thus in that place would live on no way to travel that method.

This is why making an abstract method final inward Java volition upshot inward a compile fourth dimension error. In short, travel of keywords abstract in addition to final together is illegal inward Java.

You tin verify these facts yesteryear yourself yesteryear trying to make an abstract class/method concluding inward Java. In this article, I'll exhibit you lot an illustration of both to examine higher upwards points.

Even though it may appear slowly that both these concepts are mutually exclusive, many Java progamrmers acquire confused betwixt them. They simply don't sympathise that abstract agency incomplete in addition to concluding agency complete, in addition to how a affair tin live on both consummate in addition to incomplete at the same time. It's simply a mutual feel in 1 trial you lot know the concpet inward depth.




Can nosotros brand an Abstract course of pedagogy concluding inward Java

Let's travail that, the next is a sample code which uses both abstract in addition to concluding modifier together at course of pedagogy declarations when you lot write this code inward Eclipse, the IDE volition advise this is incorrect yesteryear giving an mistake every bit shown below:

abstract final class ImAnAbstractClass{  }

Error: "The course of pedagogy ImAnAbstractClass tin live on either abstract or final, non both"

If you lot compile higher upwards Java source file using Java compiler (javac) from the dominance business you lot volition have a similar error. Eclipse uses slightly unlike compile but it follows almost all rules of Java compiler. See a Core Java majority on nub Java to larn to a greater extent than close this concept.




Can nosotros brand an Abstract method concluding inward Java

Now that, you lot know in that place is no way you lot tin brand an abstract course of pedagogy final inward Java, let's travail to brand an abstract method concluding inward Java. As I said above, this fourth dimension likewise Java compiler should complain because both final in addition to abstract are mutual exclusive keywords:

abstract class ImAbstract{  public final abstract void anAbstractMethod(); }  class ImConcrete extends ImAbstract{  @Override public void anAbstractMethod() { // TODO Auto-generated method stub  }  }

This volition reach mistake "The abstract method anAbstractMethod inward type ImAbstract tin alone laid upwards a visibility modifier, 1 of world or protected" when you lot write this code inward Eclipse, every bit shown inward the next screenshot:

you cannot brand an abstract course of pedagogy or method concluding inward Java Can you lot brand an abstract class/method concluding inward Java?


You volition likewise have the mistake "Cannot override the concluding method from ImAbstract" on the subclass, every bit shown inward the next screenshot:

you cannot brand an abstract course of pedagogy or method concluding inward Java Can you lot brand an abstract class/method concluding inward Java?



That's all close whether you lot tin brand an abstract class/method concluding inward Java or not. The brusk reply is no, you lot cannot brand them concluding in addition to long reply is what I accept explained inward this post. Since abstract in addition to concluding stand upwards for 2 mutually exclusive concepts they cannot live on used together.

Some of you lot mightiness live on wondering close variables? Can nosotros brand a concluding variable abstract inward Java? Well, a variable cannot live on abstract inward Java. The travel of abstract keyword is illegal for a variable, it tin alone live on used amongst course of pedagogy or method, thus in that place is a way you lot tin brand a concluding variable abstract inward Java. If you lot desire to larn to a greater extent than close Java fundamentals, I advise you lot read a skillful nub Java majority e.g. Core Java For the Impatient yesteryear Cay. S. Horstmann.

Similar Java Interview Questions you lot may similar to explore
Can abstract course of pedagogy accept a constructor inward Java? (answer)
Can you lot override a static method inward Java? (answer)
Can you lot overload a static method inward Java? (answer)
Can you lot override a person method inward Java? (answer)
Can you lot overload in addition to override the main() method inward Java? (answer)
Can you lot brand array volatile inward Java? (answer)
Can you lot declare a course of pedagogy static inward Java? (answer)

Thanks for reading this article, If you lot similar this interview enquiry in addition to my explanation in addition to so delight percentage amongst your friends in addition to colleagues. If you lot accept whatsoever enquiry or feedback in addition to so delight drib a comment.

Subscribe to receive free email updates:

0 Response to "Can yous brand an abstract class/method in conclusion inward Java?"

Posting Komentar