Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. 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

What is the output of the 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. Car obj = new MySuzuki();
  17. obj.startEngine();
  18. }
  19. }
Output:
MySuzuki

Sunday, May 12, 2019

what is the output of the program?

Problem statement:
what is the output of the following code?
  1. public class A {
  2.     public static void showMe() {
  3.         System.out.println("A");
  4.     }
  5. }
  6. class B extends A {
  7.     public static void showMe() {
  8.         System.out.println("B");
  9.     }
  10. }
  11. class C extends B {
  12.     public static void showMe() {
  13.         System.out.println("C");
  14.     }
  15. }
  16. class Main {
  17.     public static void main(String[] args) {
  18.         A a = new B();
  19.         a.showMe();
  20.         A a1 = new A();
  21.         a1.showMe();
  22.         B b = new C();
  23.         b.showMe();
  24.         C c = new C();
  25.         c.showMe();
  26.     }
  27. }
Output:
A
A
B
C

Thursday, May 3, 2018

Difference between final, finally and finalize()

final:
  • final is a keyword
  • final is used to apply restrictions on class, method and variable
  • final class cannot be inherited
  • final method cannot be overriden
  • final variable value cannot be changed
  1. class FinalExample {
  2. public static void main(String[] args) {
  3. final int x = 100;
  4. x = 200;// Compile Time Error
  5. }
  6. }
finally:
  • finally is a block
  • finally block always execute, whether exception is handled or not
  • in one scenario finally block doesn't execute is System.exit(0);
  1. class FinallyExample {
  2. public static void main(String[] args) {
  3. try {
  4. int x = 300;
  5. } catch (Exception e) {
  6. System.out.println(e);
  7. } finally {
  8. System.out.println("finally block is executed");
  9. }
  10. }
  11. }
finalize():
  • finalize() is a method
  • finalize is used to perform clean up processing just before object is garbage collected
  1. class FinalizeExample {
  2. public void finalize() {
  3. System.out.println("finalize called");
  4. }
  5. public static void main(String[] args) {
  6. FinalizeExample f1 = new FinalizeExample();
  7. FinalizeExample f2 = new FinalizeExample();
  8. f1 = null;
  9. f2 = null;
  10. System.gc();
  11. }
  12. }

Sunday, April 29, 2018

Can we override static method in java?

No ! we cannot override the static methods. Since static method belongs to class level not object level.

class StaticExe1 {
public static void run() {
System.out.println("run of StaticExe1");
}
public  void fly() {
System.out.println("fly of non-static Exe1");
}
}

class StaticExe2 extends StaticExe1 {
public static void run() {
System.out.println("run of StaticExe2");
}
public  void fly() {
System.out.println("fly of non-static Exe2");
}
}
class StaticExe3 extends StaticExe2 {
public static void run() {
System.out.println("run of StaticExe3");
}
public  void fly() {
System.out.println("fly of non-static Exe3");
}
}

public class StaticMethod {

public static void main(String[] args) {

StaticExe2 ex = new StaticExe3();
ex.run();
ex.fly();
}


}
Output:
run of StaticExe2

fly of non-static Exe3

Tuesday, April 24, 2018

What is the output of the program ? [based on method overriding]

  1. class SampleExec1 {
  2. void run() {
  3. System.out.println("SampleExec1 run");
  4. }
  5. }
  6. class SampleExec2 extends SampleExec1 {
  7. void run() {
  8. System.out.println("SampleExec2 run");
  9. }
  10. }
  11. public class SampleExecution {
  12. public static void main(String[] args) {
  13. SampleExec2 ex = new SampleExec1();
  14. ex.run();
  15. }
  16. }
Output: compile time error at line 13. Type mismatch: cannot convert from SampleExec1 to SampleExec2

--------------------------------------------------------------------
  1. class SampleExec1 {
  2. void run() {
  3. System.out.println("SampleExec1 run");
  4. }
  5. }
  6. class SampleExec2 extends SampleExec1 {
  7. void run() {
  8. System.out.println("SampleExec2 run");
  9. }
  10. }
  11. public class SampleExecution {
  12. public static void main(String[] args) {
  13. SampleExec2 ex = (SampleExec2 )new SampleExec1();
  14. ex.run();
  15. }
  16. }
Output: 
Exception in thread "main" java.lang.ClassCastException: SampleExec1 cannot be cast to SampleExec2 at SampleExecution.main(SampleExecution .java:14)

--------------------------------------------------------------------
  1. class SampleExec1 {
  2. void run() {
  3. System.out.println("SampleExec1 run");
  4. }
  5. }
  6. class SampleExec2 extends SampleExec1 {
  7. void run() {
  8. System.out.println("SampleExec2 run");
  9. }
  10. }
  11. public class SampleExecution {
  12. public static void main(String[] args) {
  13. SampleExec1 ex = new SampleExec2();
  14. ex.run();
  15. }
  16. }
Output: SampleExec2 run

Friday, April 20, 2018

what is cyclic inheritance in java ?

Suppose class A extends class B and class B extends class A. then it will be known as cyclic inheritance.
e.g.

[1]
public class A extends B {
// some code
}
class B extends A {
// some code
}
[2]
public class A extends B {
// some code
}
public class B extends C {
// some code
}
public class C extends A {
// some code
}
[3]
public class A extends A {
// some code
}
error: cyclic inheritance involving.

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...