Friday, April 26, 2024
HomeJavaAre you able to declare Summary Class or Methodology Remaining in Java?...

Are you able to declare Summary Class or Methodology Remaining in Java? If not, why?


If you’re questioning whether or not you need to use each summary and remaining modifier
along with a category or methodology in Java then you aren’t alone. That is one
of the regularly requested,
concept primarily based Java questions
which is someday difficult to reply, significantly if you do not have full
data of Java fundamentals. The reply of this query is
No, you can’t declare summary class or methodology remaining in Java
as a result of each summary and remaining key phrases are mutual unique. Something
which is summary can’t be remaining and vice-versa. It is like an individual can’t
be residing and useless on the identical time.
As a way to perceive this mutual
exclusiveness, it’s good to know what does
summary and
remaining
means in Java. As soon as you recognize that idea you’ll be able to purpose concerning the reply your self
and that is what you’ll be taught on this article. 

What’s summary modifier in Java?

The summary key phrase in Java is
used to symbolize in-complete issues in Java. For instance, an summary class
is taken into account in-complete and it might solely be used i.e. by creating object
upon getting prolonged the category and overridden its summary methodology to mark
the category full. 

Additionally, summary key phrase could be utilized to a category or methodology
however not on a variable as a result of it might’t be incomplete. A way is
mentioned to be be summary in the event you solely declare the strategy with out defining
it. 

Equally a category is alleged to be summary, if it comprises an summary methodology
of you simply mark it summary utilizing key phrase within the class
declaration.  

Right here is a few examples of summary class and summary methodology in Java:

public summary class AbstractClass{

     public summary void abstractMethod();

}

You’ll be able to see that we’ve used summary key phrase within the class declaration
which is necessary as a result of there may be an
summary methodology within the class
which mechanically makes the category summary. 

What’s remaining modifier in Java?

The ultimate key phrase in Java signifies an entire factor. For instance, if a
variable is remaining which suggests you can not assign it a brand new worth as soon as
created. 

Equally, if a category is remaining then you definitely can’t prolong it and if a technique is
remaining then you definitely can’t
override it to alter it operate. 

Right here is an instance of remaining class, methodology, and variable in Java:

public remaining class FinalClass{
 non-public remaining variable model = 3;

 public remaining void model(){
   return model;
 }

}

When you made a category remaining, you can not prolong it which suggests you misplaced the
energy of
inheritance

Why summary class can’t be made remaining in Java?

Now that you recognize what does summary and remaining means in Java, it is fairly
simple to resolve why we can’t make an
summary class remaining. Since summary means in-complete and remaining means full, it is logical
that any class or methodology can’t be full and in-complete on the identical
time. This implies they can’t be summary and remaining on the identical time. 
They are often both in-complete or full i.e.
summary
or
remaining however they can’t be each on the identical time.   If you happen to
attempt to make an summary class remaining in Java
, the compiler will generate error as proven under:
package deal device;


/**
* 
* A easy Java Program to exhibit {that a} class
* can't be each summary and remaining on the identical time. 
*/
public summary remaining class Whats up {

public static void most important(String[] args) {
  // your superior code

}

}

javac /tmp/iSrGFqg8oS/Whats up.java
/tmp/iSrGFqg8oS/Whats up.java:8: 
error: unlawful mixture of modifiers: summary and remaining
public summary remaining class Whats up {
                      ^
1 error

You’ll be able to see that
utilizing each summary and remaining modifier collectively is an unlawful
mixture in Java
and you’ll at all times get a compilation error while you use these two
key phrases collectively, regardless of whether or not you might be utilizing them with a category, a
methodology or whereas declaring a variable in Java. 

Can we declare abstract class or method final in Java? If not, why?

That is all about
whether or not you’ll be able to declare an summary class or methodology remaining in Java.
No, you can not. As we’ve seen, each summary and remaining key phrase are
mutual unique and you can not use them collectively. It can end in compile
time error as a result of summary means in-complete and remaining means full, and
any class or methodology can’t be be in-complete and full on the identical time.
They are going to be both incomplete or full however can’t be each on the identical
time. 

Different Core Java Interview Questions you could like:

Thanks for studying this text to date. If you happen to like this core Java interview query and my rationalization then please share with your pals and colleagues. You probably have any questions or suggestions then please drop a observe.

P.S. – If you’re making ready for Java interview and want steering then you too can checkout this record of finest Java interview books and programs to organize nicely in your subsequent Java Interviews. In these articles one can find superior assets which covers Java in its size and breadth and share widespread interview questions. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments