Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts

Saturday, August 24, 2019

What is the output of the given java code?

Problem statement:
Given java code, what will be the output of it?
  1. interface Car {
  2. public void startEngine();
  3. }
  4. class MySuzuki implements Car {
  5. public void startEngine() {
  6. System.out.println("MySuzuki");
  7. }
  8. }
  9. class MyFerrari implements Car {
  10. public void startEngine() {
  11. System.out.println("MyFerrari");
  12. }
  13. }
  14. public class OOPSExample {
  15. public static void main(String[] args) {
  16. MyFerrari obj = new MySuzuki();
  17. obj.startEngine();
  18. }
  19. }
Output:
Compile time error at line 16, due to Type mismatch: cannot convert from MySuzuki to MyFerrari

Sunday, May 12, 2019

what will be the output of the following code?

Problem statement:
what is the output of the following code?
  1. interface Test1 {
  2.     default void run() {
  3.         System.out.println("test1");
  4.     }
  5. }
  6. interface Test2 {
  7.     default void run() {
  8.         System.out.println("test2");
  9.     }
  10. }
  11. public class A implements Test1, Test2 {
  12.     public void run() {
  13.         System.out.println("A");
  14.     }
  15.     public static void main(String args[]) {
  16.         A a = new A();
  17.         a.run();
  18.         Test1 t1 = new A();
  19.         t1.run();
  20.         Test2 t2 = new A();
  21.         t2.run();
  22.     }
  23. }
Output:
A
A
A

Wednesday, May 23, 2018

how marker interface is handled by JVM in java?

Problem statement: how marker interface or tag interface works in java?

) Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it does some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.


Monday, April 23, 2018

Can many classes implements same interface ?

  1. public class MultipleClass implements X{}
  2. class MultipleClass1 implements X{}
  3. class MultipleClass2 implements X{}
  4. class MultipleClass3 implements X{}
  5. interface X {}
Yes ! we can implements same interface in multiple classes.

Can a class implements multiple interface ?


  1. interface MultipleInterface {}
  2. interface MultipleInterface1 {}
  3. interface MultipleInterfac2 {}
  4. public class UserMain implements MultipleInterface, MultipleInterface1, MultipleInterfac2 {
  5. // code here
  6. }
Yes ! we can implements multiple interface in a class.

Can we have a constructor in an interface ?

  1. interface MyInterfaceExe {
  2. public void MyInterfaceExe (){
  3. // code here
  4. }
  5. }
No ! we cannot have a constructor in an interface. Compiler will give an error. 

  1. interface MyInterfaceExe {
  2. public static void MyInterfaceExe (){
  3. // code here
  4. };
  5. }
       ----------------------------------------------------
  1. interface MyInterfaceExe {
  2. public default void MyInterfaceExe (){
  3. // code here
  4. };
  5. }
In above case, Yes ! we can have a constructor only if we have either static or default keyword in constructor bodies ending with (;) semicolon.

Can we create an instance of an interface ?

  1. interface MyInterfaceExe {
  2. MyInterfaceExe exe = new MyInterfaceExe();
  3. }
No ! we cannot instantiate an interface. Compiler will give an error saying that cannot instantiate the type of  MyInterfaceExe.
  1. interface MyInterfaceExe {
  2. MyInterfaceExe exe = new MyInterfaceExe(){ };
  3. }
Yes ! we can create the object of an interface using anonymous class as above.

Can an interface have static keyword in method with bodies ?

  1. interface MyInterfaceExe {
  2. public static void run(){
  3. // code here
  4. }
  5. }
Yes ! we can have static keyword in method with bodies.

Can an interface have default keyword in method with bodies ?

  1. interface MyInterfaceExe {
  2. public default void run(){
  3. // code here
  4. }
  5. }
Yes ! we can have default keyword in method with bodies.

Can an interface have both default and static keyword in method with bodies ?

  1. interface MyInterfaceExe {
  2. public default static void run(){
  3. // code here
  4. }
  5. }
No ! we can not have both default and static keyword in method with body. Compiler will give an error saying that illegal combination of modifier at line 2.

What is the default nature of an interface method's ?

  1. interface MyInterfaceExe {
  2. // Any number of abstract method declarations
  3. void run();
  4. // is equivalent to public abstract void run();
  5. int test();
  6. // is equivalent to public abstract int test();
  7. }
 The default nature of an interface method's is both public & abstract.

What is the default modifier of field / variable in an interface ?

  1. interface MyInterfaceExe {
  2. // Any number of final, static fields / variables
  3. int x = 10;
  4. // is equivalent to final static int x  = 10;
  5. String s = "ishaan";
  6. // is equivalent to final static String s = "ishaan";
  7. }
Note: The default nature of an interface field's / variable's  is public final static.

Where is the byte code generated by an interface ?

  1. interface MyInterfaceExe {
  2. // Any number of final, static fields / variables
  3. // Any number of abstract method declarations
  4. }
Byte code generated by an interface appears in .class file.

What is the extension of the file to save an interface program ?

  1. interface MyInterfaceExe {
  2. // Any number of final, static fields / variables
  3. // Any number of abstract method declarations
  4. }
The extension is .java for interface written program.
Lets save as: MyInterfaceExe.java

How to declare an interface ?

  1. interface MyInterfaceExe {
  2. // Any number of final, static fields / variables
  3. // Any number of abstract method declarations
  4. }
we must use interface keyword to declare an interface followed by identifier name.

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...