Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Output of Java program | Set 27
Next article icon

Output of Java Program | Set 20 (Inheritance)

Last Updated : 14 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
Prerequisite - Inheritance in Java Predict the output of following Java Programs. Program 1 : JAVA
class A {     public A(String s)      {         System.out.print("A");     } }  public class B extends A  {     public B(String s)      {         System.out.print("B");     }     public static void main(String[] args)      {         new B("C");         System.out.println(" ");     } } 
Output: Compilation fails
prog.java:12: error: constructor A in class A cannot be applied to given types;      {      ^    required: String    found: no arguments    reason: actual and formal argument lists differ in length  1 error  
Explanation: The implied super() call in B's constructor cannot be satisfied because there isn’t a no-arg constructor in A. A default, no-arg constructor is generated by the compiler only if the class has no constructor defined explicitly.For detail See - Constructors in Java Program 2 : JAVA
class Clidder  {     private final void flipper()      {         System.out.println("Clidder");     } }  public class Clidlet extends Clidder  {     public final void flipper()      {         System.out.println("Clidlet");     }     public static void main(String[] args)      {         new Clidlet().flipper();     } } 
Output:
Clidlet
Explanation: Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs. Program 3 : JAVA
class Alpha  {     static String s = " ";     protected Alpha()      {         s += "alpha ";     } } class SubAlpha extends Alpha  {     private SubAlpha()      {         s += "sub ";     } }  public class SubSubAlpha extends Alpha  {     private SubSubAlpha()      {         s += "subsub ";     }     public static void main(String[] args)      {         new SubSubAlpha();         System.out.println(s);     } } 
Output:
alpha subsub
Explanation: SubSubAlpha extends Alpha! Since the code doesnt attempt to make a SubAlpha, the private constructor in SubAlpha is okay. Program 4 : JAVA
public class Juggler extends Thread  {     public static void main(String[] args)      {         try          {             Thread t = new Thread(new Juggler());             Thread t2 = new Thread(new Juggler());         }         catch (Exception e)          {             System.out.print("e ");         }     }     public void run()      {         for (int i = 0; i < 2; i++)          {             try              {                 Thread.sleep(500);             }             catch (Exception e)              {                 System.out.print("e2 ");             }             System.out.print(Thread.currentThread().getName()+ " ");         }     } } 
Output: No Output Explanation: In main(), the start() method was never called to start ”t” and ”t2”, so run() never ran. For detail: See Multithreading in Java   Program 5 : JAVA
class Grandparent  {     public void Print()      {         System.out.println("Grandparent's Print()");      }  }  class Parent extends Grandparent  {     public void Print()      {         System.out.println("Parent's Print()");      }  }  class Child extends Parent  {     public void Print()        {         super.super.Print();         System.out.println("Child's Print()");      }  }  public class Main  {     public static void main(String[] args)      {         Child c = new Child();         c.Print();      } } 
Output: Compiler Error in super.super.Print() Explanation: In Java, it is not allowed to do super.super. We can only access Grandparent’s members using Parent. See Inheritance in Java Related Article: Quiz on Inheritance in Java

Next Article
Output of Java program | Set 27

P

Pavan Gopal Rayapati
Improve
Article Tags :
  • Java
  • Java-Output
Practice Tags :
  • Java

Similar Reads

  • Output of Java Program | Set 2
    Predict the output of the following Java programs. Question 1: Java Code package main; class Base { public void Print() { System.out.println("Base"); } } class Derived extends Base { public void Print() { System.out.println("Derived"); } } class Main { public static void DoPrint(
    3 min read
  • Output of Java program | Set 26
    Ques1: What is the output of this program? [GFGTABS] Java class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.
    4 min read
  • Output of Java program | Set 28
    Question 1. What is the output of the following question? class Test { int a = 10; static int b = 20; public static void main(String[] args) { Test t1 = new Test(); t1.a = 100; t1.b = 200; Test t2 = new Test(); System.out.println("t1.a =" + t1.a + " t1.b =" + t1.b); System.out.pr
    4 min read
  • Output of Java program | Set 27
    Ques1. What is the output of the following? import java.util.*; public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } } Options : A. The program has a compile error because the size of the array wasn't specified when dec
    3 min read
  • Output of Java programs | Set 29
    Question 1. What is the output of the following question? class Test1 { public static void main(String[] args) { int String = 65; int Runnable = 97; System.out.print(String + " : " + Runnable); } } Option A) Error B) A : a C) 65 : 97 D) None Output: C Explanation : We can use all predefine
    2 min read
  • Output of Java Programs | Set 50
    Q 1. What is the output of this program? [GFGTABS] Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } [/GFGTABS]Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run
    3 min read
  • Output of Java Programs | Set 30
    QUE.1 What is the output of this program ? public class Prg { public static void main(String args[]) { System.out.print('A' + 'B'); } } OPTION a) AB b) 195 c) 131 d) Error Answer: c Explanation: Here, ‘A’ and ‘B’ are not strings they are characters. ‘A’ and ‘B’ will not concatenate. The ASCII of the
    2 min read
  • Output of Java Programs | Set 12
    1) What is the output of the following program? Java Code public class Test implements Runnable { public void run() { System.out.printf("%d", 3); } public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(new Test()); thread.start(); System.out.printf
    5 min read
  • Output of Java Programs | Set 42 (Arrays)
    Prerequisite : Java Arrays Question 1. What is the output of this question? class Test1 { public static void main(String[] args) { int arr[] = new int[5]; int arr2[] = new int['a']; byte bt = 10; int arr3[] = new int[bt]; System.out.println(arr.length); System.out.println(arr2.length); System.out.pr
    3 min read
  • Output of Java Program | Set 3
    Predict the output of the following Java Programs: Example1: Java Code // filename: Test.java class Test { // Declaring and initializing integer variable int x = 10; // Main driver method public static void main(String[] args) { // Creating an object of class inside main() Test t = new Test(); // Pr
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences