ADVERTISEMENT
ADVERTISEMENT

Java find output programs (final Keyword) | set 2

Find the output of Java programs | final Keyword | Set 2: Enhance the knowledge of Java final Keyword concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 02, 2021

Question 1:

final class Sample {
  static void sayHello() {
    System.out.println("Hello World");
  }
}

public class FinalEx extends Sample {
  public static void main(String[] args) {
    sayHello();
  }
}

Output:

/FinalEx.java:7: error: cannot inherit from final Sample
public class FinalEx extends Sample {
                             ^
1 error

Explanation:

The above program will generate syntax error because we cannot inherit a final class in Java.

Question 2:

class Sample {
  final static void sayHello() {
    System.out.println("Hello World");
  }
}

public class FinalEx extends Sample {
  public static void main(String[] args) {
    sayHello();
  }
}

Output:

Hello World

Explanation:

In the above program, we created two classes Sample and FinalEx. The Sample class contains a static final method sayHello(). Then we inherited the Sample class into FinalEx class then we called sayHello() method in the main() method of FinalEx class that will print "Hello World" on the console screen.

Question 3:

class Sample {
  void sayHello() {
    System.out.println("Hello World");
  }
}

public class FinalEx {
  public static void main(String[] args) {
    final Sample S = new Sample();
    S.sayHello();
  }
}

Output:

Hello World

Explanation:

In the above program, we created a class Sample that contains the method sayHello(). Then we created a FinalEx class that contains the main() method.

In the main() method we created an object of Sample class using the final keyword and then called the sayHello() method that will print "Hello World" on the console screen.

Question 4:

class Sample {
  final void fun() {
    System.out.println("Sample.fun() called");
  }
}

public class FinalEx extends Sample {
  void fun() {
    System.out.println("FinalEx.fun() called");
  }
  public static void main(String[] args) {
    Sample S = new Sample();
    S.fun();
  }
}

Output:

/FinalEx.java:8: error: fun() in FinalEx cannot override fun() in Sample
  void fun() {
       ^
  overridden method is final
1 error

Explanation:

The above program will generate syntax error because here we override method fun() of Sample class in FinalEx class. But we cannot override the final method in java.

Question 5:

class Sample {
  final void fun() {
    System.out.println("Sample.fun() called");
  }
}

public class FinalEx {
  void fun() {
    System.out.println("FinalEx.fun() called");
  }
  public static void main(String[] args) {
    Sample S = new Sample();
    S.fun();
  }
}

Output:

Sample.fun() called

Explanation:

In the above program, we created two classes Sample and FinalEx. The Sample class contains a final method fun() and FinalEx class contains two methods fun() and main().

In the main() method we created the object S of Sample class then called fun() method of Sample class that will print "Sample.fun() called" on the console screen.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.