1) When to use abstract class and when to use interface in Java?
Please refer this link When we use interface and abstract class in java?
2) What modifiers may be used with an interface declaration?
public & abstract.
3) Can Abstract class have a Constructors? Can interfaces have a Constructors?
Abstract class's can have a constructor but you can not access it through the object, since you can not instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.
public abstract class AbsExample {
Interface can not have the constructor.
4) If interface and abstract class have same methods and those methods contain no implementation, which one would you prefer?
Obviously you should go for an interface. Implementing an interface is for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass.
5) Can abstract class implements interface in Java? does they require to implement all methods?
Yes, abstract class can implement interface by using implements keyword. Since they are abstract ,they don't need to implement all methods, in sub class that can extends abstract class to need to implement methods.
Example:
public interface example1{
In the above code, won't give any error. Because in abstract class it's not necessary to implement interface abstract methods. In sub class you have to implement all methods.
6) Can abstract class be final in Java?
No, abstract class can not be final in Java. Making them final will stop abstract class from being extended.
7) Can an interface be final in Java?
Not possible, doing so will result compilation error.
8) Can abstract class have static methods in Java?
Yes, abstract class can declare and define static methods.
9) Can you create instance of abstract class in Java?
No, you can not make instance of the abstract class in Java. because it's incomplete.
10) Can abstract class contains main method?
Yes, abstract class can contain main method, its just another static method.
11) Is it necessary for abstract class to have abstract method?
No, it's not necessary for abstract class to have abstract method.
12) Like classes, does interfaces also extend Object class by default ?
No, interfaces don't extend Object class.
13) Can we declare abstract methods as synchronized?
No, we can not declare abstract methods as synchronized. But overridden methods you can declare as synchronized.
14) What is the output of below code?
class Example{
Answer:- Compile time error. If there is any abstract method in a class, that class must be abstract.
15) If we declare a concrete method in an interface , what will happen ?
public interface InterfaceExample {
public void display () { //compile time error
}
}
In an interface all methods should be abstract, if we declare concrete method , it will give compile time error that "Abstract methods do not specify a body".
Please refer this link When we use interface and abstract class in java?
2) What modifiers may be used with an interface declaration?
public & abstract.
3) Can Abstract class have a Constructors? Can interfaces have a Constructors?
Abstract class's can have a constructor but you can not access it through the object, since you can not instantiate abstract class. To access the constructor create a sub class and extend the abstract class which is having the constructor.
public abstract class AbsExample {
public AbsExample() {
System.out.println("Abstract class constructor example");
}
}
public class Test extends AbsExample {
public static void main (String[] args) {
Test test = new Test();
}
}Interface can not have the constructor.
4) If interface and abstract class have same methods and those methods contain no implementation, which one would you prefer?
Obviously you should go for an interface. Implementing an interface is for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass.
5) Can abstract class implements interface in Java? does they require to implement all methods?
Yes, abstract class can implement interface by using implements keyword. Since they are abstract ,they don't need to implement all methods, in sub class that can extends abstract class to need to implement methods.
Example:
public interface example1{
public String getName();
}
public abstract class AbstractEx implements example1 {
}
public class ExampleOfInterface extends AbstractEx{
public String getName() {
return "Anil";
}
}
6) Can abstract class be final in Java?
No, abstract class can not be final in Java. Making them final will stop abstract class from being extended.
7) Can an interface be final in Java?
Not possible, doing so will result compilation error.
8) Can abstract class have static methods in Java?
Yes, abstract class can declare and define static methods.
9) Can you create instance of abstract class in Java?
No, you can not make instance of the abstract class in Java. because it's incomplete.
10) Can abstract class contains main method?
Yes, abstract class can contain main method, its just another static method.
11) Is it necessary for abstract class to have abstract method?
No, it's not necessary for abstract class to have abstract method.
12) Like classes, does interfaces also extend Object class by default ?
No, interfaces don't extend Object class.
13) Can we declare abstract methods as synchronized?
No, we can not declare abstract methods as synchronized. But overridden methods you can declare as synchronized.
14) What is the output of below code?
class Example{
abstract void start();
}
Answer:- Compile time error. If there is any abstract method in a class, that class must be abstract.
15) If we declare a concrete method in an interface , what will happen ?
public interface InterfaceExample {
public void display () { //compile time error
}
}
In an interface all methods should be abstract, if we declare concrete method , it will give compile time error that "Abstract methods do not specify a body".
Helpful post, thanks for sharing this coding. It is really useful.
ReplyDeleteSpring Training in Chennai | Hibernate Training in Chennai | Struts Training in Chennai
Hey Anil Nivargi,
ReplyDeleteThank you! Thank you! Thank you! Your blog was a total game changer!
I am going to
implement the blackbox class "messagePool". Details are as below. I
just want to implement it so that the whole application can run.
// Call messagePool
Java Code:
Dac dac =
GenericDac.getInstance();
String result =
dac.getMessagePool().getMessage(languageCode, key);
public abstract interface Dac extends java.io.Serializable{
public abstract void init(javax.servlet.ServletContext
arg0, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3,
java.lang.String arg4) throws com.jos.adf3.core.exception.DacInitException;
public abstract com.pool.MessagePool getMessagePool();
}
public class GenericDac implements Dac{
private com.pool.MessagePool messagePool;
}
Super likes !!! for this
amazing post. I thinks
everyone should bookmark this.
Regards,
Morgan
Nice blog..It was informative and very helpful..Thanks for sharing.keep update selenium training in velachery
ReplyDeletebest selenium training in chennai
ReplyDeleteI observed this website on internet
very unique and interesting. - Learn Digital Academy
360DigiTMG, Indore is a leading solutions provider of Training and Consulting to assist students, professionals by delivering top-notch, world-class classroom and online training. It offers courses in data science training in indore.
ReplyDeletelisans satın al
ReplyDeleteen son çıkan perde modelleri
minecraft premium
uc satın al
nft nasıl alınır
yurtdışı kargo
özel ambulans
en son çıkan perde modelleri
Good content. You write beautiful things.
ReplyDeletesportsbet
hacklink
korsan taksi
mrbahis
sportsbet
vbet
taksi
hacklink
mrbahis
Good text Write good content success. Thank you
ReplyDeletebetmatik
kralbet
poker siteleri
mobil ödeme bahis
slot siteleri
tipobet
betpark
kibris bahis siteleri