You’ll be able to evaluate these questions earlier than going for Java interview, each telephonic and face-to-face spherical. They’re additionally very helpful if you’re getting ready for Oracle Java certification e.g. OCAJP.
You’ll be able to reply nearly all of those questions when you’ve got a few years of expertise in core Java however if you happen to battle to reply most of them, possibly it is time to revise these ideas by studying a superb core Java e-book e.g. The Java Full reference.
20 Summary class and Technique Interview Questions for two to five Years Skilled
So, with none additional ado, right here is my a listing of 20 summary class and technique associated interview questions from numerous Java interviews which is able to show you how to to know summary class and technique in Java in nice depth. These questions aren’t simply vital from interview perspective but in addition to study one of many vital core Java function, I imply abstraction, particularly summary class and strategies in Java.
1. What’s an summary class in Java? (detailed reply)
An summary class in Java is a particular class which can’t be instantiate. In different phrases, it means an incomplete class in Java. You make a category summary to make sure that anybody who desires to make use of it should present a concrete implementation of it. There are a number of examples of summary lessons in java e.g. AbstractList or AbstractCollection.
Previous to Java 8 they’re used to offer skeleton implementation of corresponding interface e.g. AbstractList supplies an implementation of Record interface whereas AbstractCollection supplies implementation of Assortment interface.
Now, it turn out to be simpler for any class to increase AbstractList as an alternative of implementing Record interface. It simply must override the tactic it need as an alternative of implementing each strategies outlined in Record interface. From Java 8 onwards, you do not want this as a result of you may as well present default implementation utilizing default strategies within the interface itself.
2. What’s an summary technique in Java? (detailed reply)
An summary technique is a particular technique in Java which does not have a technique physique. It simply declare a technique with out defining it. An summary technique can exists in interface or summary class they usually had been added to outline an API. The concrete implementation of summary technique is offered by overriding it in subclass. An instance of summary technique is run() technique of java.lang.Thread class.
3. What’s distinction between a concrete class and an summary class in Java? (detailed reply)
The important thing distinction between an summary class and concrete class is that you may create occasion of concrete class however you can not instantiate an summary class in Java. The Java compiler would not enable that as a result of an summary class is taken into account incomplete. It could or could not comprise summary technique however you have to lengthen it to offer concrete implementation which will be instantiated.
Sure, you may declare and outline a constructor inside an summary class in Java, however you can not instantiate an summary class, that might lead to compile time error. The constructor is used to initialize widespread fields outlined within the summary class and it’s invoked when a shopper create occasion of a concrete implementation of summary class in Java.
No, you can not make an summary class last in Java as a result of each summary and last key phrase are mutually unique. This implies you can not use them collectively in a category declaration. It’s also a standard sense as a result of summary class characterize an incomplete class which might solely be accomplished by extending it and a last class characterize a concrete class which shouldn’t be prolonged.
Now, you may’t make an summary technique last in Java as a result of similar causes you may’t make summary class last. Just like summary class, an summary technique can be incomplete and you have to override to finish it. Since, you can not override a last technique in Java, it make no sense to make an summary technique last in Java. Once more, it is a compile time error to take action.
That is the follow-up of the primary query, when candidate says that an summary class can have constructors outlined, Interviewer ask whether it is doable to instantiate an summary class in Java? Reply is No, it is not doable even that summary class can have constructors. It is a compile time error attempting to create object of summary class in Java.
Sure, an summary can have static strategies in Java, there isn’t any downside with that. Here’s a code instance to show this level:
public summary class Shopper {
public summary void eat();
public static void fundamental(String args[]) {
System.out.println("Inside summary class");
}
}
Sine fundamental technique is a static technique in Java, it proves the purpose that summary class can have static technique in Java.
Sure, an summary class could comprise last technique, there may be nothing unsuitable with that. Here’s a code instance to show that time
static summary class Poppy {
public summary void dance();
}
Once you compile this code, there isn’t any compile time error which proves that you may have a last technique in summary class in Java.
This is without doubt one of the difficult query in Java. It’s also requested as follow-up of earlier query, candidate assume if an summary class comprise last technique then why not last class comprise summary technique, however they fail to acknowledge that after make a technique summary in Java, the enclosing class routinely turn out to be an summary class.
Since you can not make an summary class last in Java, it turns into unlawful. Therefore, it is not doable to have an summary technique in last class. It’s also a little bit of widespread sense, a last class can’t be prolonged and to make an summary technique helpful, you have to override it, however you can not override a technique with out extending that class.
11. Are you able to make an summary class static in Java? (detailed reply)
The reply is each, Sure and No. You can not make a high degree summary class static in Java as mentioned in earlier article, however you may all the time make a nested class static in Java as proven in observe instance:
public summary class TopLevel {
}
public class Container {
public summary class Part {
public summary void paint();
}
}
You’ll be able to compile this program utilizing javac to confirm that fist gives you compile time error however second will simply compile fantastic.
Sure, an summary class can implement an interface in Java. There is no such thing as a downside with that. It may implement summary strategies inherited from interface or go away them un-implemented as a result of its an summary class, each are fantastic.
13. What’s using constructor in summary class in Java? (detailed reply)
That is one other fascinating and difficult core Java query, normally requested as follow-up of query no 1 or 7 i.e. whether or not summary class can have constructor or not and may you instantiate summary class in Java or not?
14. Can an summary class have last variables in Java? (detailed reply)
Sure, an summary class have last variables in Java. This query is normally requested to confuse candidates as a result of an summary class can’t be last in Java, however it will probably comprise each last variables in addition to last strategies as proven in following instance:
static summary class Compiler {
public last int model = 1;
public last int getVersion() {
return model;
}
public summary void compile();
}
You’ll be able to see that above code accommodates each last variables and last strategies inside summary class in Java.
15. Can an summary class have static variables in Java? (detailed reply)
Sure, an summary class could comprise static variable in Java, there isn’t any downside with that. IT may even comprise static strategies as proven in following instance:
static summary class JVM {
public static last int model = 1;
public static last int getVersion() {
return model;
}
public summary void execute();
}
It typically a superb observe to make static variable last in Java to stop somebody altering its worth. It is a lot safer strategy.
16. Are you able to make a category summary with none summary technique? (detailed reply)
Sure, Java specification permits you to mark a category summary even when it would not comprise an summary technique as proven in following instance :
static summary class JVM {
public void data() {
System.out.println("I'm a JVM");
}
}
Like another summary class, you can not instantiate this class until you create a concrete class by extending it.
17. What’s distinction between a standard technique and summary technique in Java? (detailed reply)
The important thing distinction is that an summary technique would not have physique, it solely have declarations like:
public summary int depend();
whereas a concrete technique all the time have technique physique as proven under:
public int depend(){
return depend;
}
Additionally, when an summary technique can solely exists in an summary class however a concrete technique can exists in each regular class in addition to on summary class.
18. Is it doable to inherit from a number of summary lessons in Java? (detailed reply)
No, as per Java specification a number of inheritance isn’t allowed, which implies a category can solely lengthen one class, therefore its not doable to inherit from a number of summary class in Java. A category can solely lengthen at most one summary class in Java.
19. Why summary class can’t be static in Java? (detailed reply)
The query is unsuitable, an summary class will be static. Solely a high degree summary class can’t be static as a result of it would not make sense as mentioned right here, however you may make a nested summary class static in Java as proven under:
public class Outer {
static summary class Internal {
}
}
The code will compile fantastic which proves {that a} nested summary class will be static in Java.
This is without doubt one of the most regularly requested query on this subject. I’ve seen summary class vs interface query so many instances in my profession and fortunately it offers you good alternative to showcase your OOP expertise.
The important thing distinction is that interface can solely comprise public strategies and that is why they’re used to specify API however summary class could comprise non-public strategies or implementations in Java. A category may also implement a number of interface however just one summary class in Java. You’ll be able to learn a extra detailed reply of this query right here and right here.
That is all about what’s summary class and technique in Java with instance as effectively a few the regularly requested questions on summary class and strategies in Java. These are crucial ideas, each from Java interview perspective and Java certification perspective e.g. OCAJP or OCPJP.
If you do not know solutions of those questions, possibly it is a good time to revise these ideas by studying a superb e-book on core Java e.g. Java: Full Reference, Ninth Version or trying out official Java documentation.
Different Interview Questions articles it’s possible you’ll like