The Wayback Machine - https://web.archive.org/web/20240910223314/https://www.geeksforgeeks.org/output-java-program-set-20-inheritance/
Open In App

Output of Java Program | Set 20 (Inheritance)

Last Updated : 14 Aug, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite – Inheritance in Java

Predict the output of following Java Programs.
Program 1 :




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 :




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 :




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 :




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 :




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



Similar Reads

Output of Java Program | Set 1
Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java Code // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + &amp;quot; &amp;quot; + t.y); } } Output: 0 0 In Java, a protected member is accessible in all cl
3 min read
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(&quot;Base&quot;); } } class Derived extends Base { public void Print() { System.out.println(&quot;Derived&quot;); } } class Main { public static void DoPrint(Base o) { o.Print(); } public static void main(Str
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(); // Printing the value inside the object by // above cre
3 min read
Output of Java Program | Set 4
Predict the output of following Java Programs: Question 1 // file name: Main.java class Base { protected void foo() {} } class Derived extends Base { void foo() {} } public class Main { public static void main(String args[]) { Derived d = new Derived(); d.foo(); } } Output: Compiler Error foo() is protected in Base and default in Derived. Default a
2 min read
Output of Java Program | Set 11
Predict the output of following Java programs: Question 1 : public class Base { private int data; public Base() { data = 5; } public int getData() { return this.data; } } class Derived extends Base { private int data; public Derived() { data = 6; } private int getData() { return data; } public static void main(String[] args) { Derived myData = new
4 min read
Output of Java program | Set 16 (Threads)
Prerequisites: multi-threading in java , Life cycle and states of a thread , Joining threads in java , start() method in java 1) What could be the output of the following program? public class Test implements Runnable { public void run() { System.out.printf(&quot;GFG &quot;); System.out.printf(&quot;Geeks &quot;); } public static void main(String[]
4 min read
Output of Java program | Set 22 (Overloading)
Prerequisite - Overloading in Java1) What is the output of the following program? Java Code public class Test { public int getData() //getdata() 1 { return 0; } public long getData() //getdata 2 { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData()); } } a) 1 b) 0 c) Runtime error d) Compilat
3 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(&quot;x[0] is &quot; + x[0]); } } Options : A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime err
3 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(&quot;t1.a =&quot; + t1.a + &quot; t1.b =&quot; + t1.b); System.out.println(&quot;t2.a =&quot; + t2.a + &quot; t2.b =
4 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 + &quot; &quot; + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); } } [/GFGTABS]a)
4 min read
Output of C++ Program | Set 1
Predict the output of below C++ programs. Question 1 // Assume that integers take 4 bytes. #include&lt;iostream&gt; using namespace std; class Test { static int i; int j; }; int Test::i; int main() { cout &lt;&lt; sizeof(Test); return 0; } Output: 4 (size of integer) static data members do not contribute in size of an object. So 'i' is not consider
1 min read
Output of C Program | Set 17
Predict the output of following C programs. Question 1 #include&lt;stdio.h&gt; #define R 10 #define C 20 int main() { int (*p)[R][C]; printf(&quot;%d&quot;, sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de-referenced, hence it yields type of the object. In t
2 min read
Output of C++ Program | Set 3
Predict the output of below C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; using namespace std; class P { public: void print() { cout &lt;&lt;&quot; Inside P::&quot;; } }; class Q : public P { public: void print() { cout &lt;&lt;&quot; Inside Q&quot;; } }; class R: public Q { }; int main(void) { R r; r.print(); return 0; } Output: Insi
2 min read
Output of C Program | Set 18
Predict the output of following C programs.Question 1 C/C++ Code #include&lt;stdio.h&gt; int fun() { static int num = 40; return num--; } int main() { for(fun(); fun(); fun()) { printf(&quot;%d &quot;, fun()); } getchar(); return 0; } Output: 38 35 32 29 26 23 20 17 14 11 8 5 2Since num is static in fun(), the old value of num is preserved for subs
2 min read
Output of C Program | Set 19
Predict the outputs of following program. Difficulty Level: Rookie Question 1 #include &lt;stdio.h&gt; int main() { int a = 10, b = 20, c = 30; if (c &gt; b &gt; a) { printf(&quot;TRUE&quot;); } else { printf(&quot;FALSE&quot;); } getchar(); return 0; } Output: FALSE Let us consider the condition inside the if statement. Since there are two greater
1 min read
Output of C Program | Set 20
Predict the outputs of following C programs. Question 1 int main() { int x = 10; static int y = x; if(x == y) printf(&quot;Equal&quot;); else if(x &gt; y) printf(&quot;Greater&quot;); else printf(&quot;Less&quot;); getchar(); return 0; } Output: Compiler Error In C, static variables can only be initialized using constant literals. See this GFact fo
2 min read
Output of C++ Program | Set 4
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; int x = 10; void fun() { int x = 2; { int x = 1; cout &lt;&lt; ::x &lt;&lt; endl; } } int main() { fun(); return 0; } Output: 10 If Scope Resolution Operator is placed before a variable name then the global variable is referen
2 min read
Output of C++ Program | Set 5
Difficulty Level: RookiePredict the output of below C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; using namespace std; class Test { int value; public: Test(int v); }; Test::Test(int v) { value = v; } int main() { Test t[100]; return 0; } Output: Compiler error The class Test has one user defined constructor "Test(int v)" that expects
2 min read
Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; class Test { int value; public: Test (int v = 0) {value = v;} int getValue() { return value; } }; int main() { const Test t; cout &lt;&lt; t.getValue(); return 0; } Output: Compiler Error. A const object cannot call a non-const function. The above cod
2 min read
Output of C Program | Set 21
Predict the output of following C programs. Question 1 C/C++ Code #include&amp;lt;stdio.h&amp;gt; #define fun (x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i &amp;lt; t; i++) printf(&amp;quot;GeeksforGeeks\n&amp;quot;); return 0; } Output: Compiler Error There is an extra space in macro declaration which causes fun to be replaced by (x)
2 min read
Output of C++ Program | Set 7
Predict the output of following C++ programs. Question 1 class Test1 { int y; }; class Test2 { int x; Test1 t1; public: operator Test1() { return t1; } operator int() { return x; } }; void fun ( int x) { }; void fun ( Test1 t ) { }; int main() { Test2 t; fun(t); return 0; } Output: Compiler Error There are two conversion operators defined in the Te
2 min read
Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 template &lt;class S, class T&gt; class Pair { private: S x; T y; /* ... */ }; template &lt;class S&gt; class Element { private: S x; /* ... */ }; int main () { Pair &lt;Element&lt;int&gt;, Element&lt;char&gt;&gt; p; return 0; } Output: Compiler Error: '>>' should be '> >' within a nested tem
2 min read
Output of C++ Program | Set 8
Predict the output of following C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; class Test1 { int x; public: void show() { } }; class Test2 { int x; public: virtual void show() { } }; int main(void) { cout&lt;&lt;sizeof(Test1)&lt;&lt;endl; cout&lt;&lt;sizeof(Test2)&lt;&lt;endl; return 0; } Output: 4 8 There is only one differ
2 min read
Output of C Program | Set 22
Predict the output of following C programs.Question 1: C/C++ Code #include&lt;stdio.h&gt; int main() { enum channel {star, sony, zee}; enum symbol {hash, star}; int i = 0; for(i = star; i &lt;= zee; i++) { printf(&quot;%d &quot;, i); } return 0; } Output: compiler error: redeclaration of enumerator 'star' In the above program, enumeration constant
2 min read
Output of C++ Program | Set 10
Predict the output of following C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; #include&lt;string.h&gt; using namespace std; class String { char *p; int len; public: String(const char *a); }; String::String(const char *a) { int length = strlen(a); p = new char[length +1]; strcpy(p, a); cout &lt;&lt; &quot;Constructor Called &quot; &lt;
2 min read
Output of C Program | Set 23
Predict the output of following C Program. #include &lt;stdio.h&gt; #define R 4 #define C 4 void modifyMatrix(int mat[][C]) { mat++; mat[1][1] = 100; mat++; mat[1][1] = 200; } void printMatrix(int mat[][C]) { int i, j; for (i = 0; i &lt; R; i++) { for (j = 0; j &lt; C; j++) printf(&quot;%3d &quot;, mat[i][j]); printf(&quot;\n&quot;); } } int main()
2 min read
Output of C Program | Set 24
Predict the output of following C programs:Difficulty Level: Rookie Question 1 C/C++ Code #include&lt;stdio.h&gt; int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf (&quot;ptr2 - ptr1 = %d\n&quot;, ptr2 - ptr1); printf (&quot;(char*)ptr2 - (char*) ptr1 = %d&quot;, (char*)ptr2 - (char*)ptr1); getchar(); r
3 min read
Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; class Point { private: int x; int y; public: Point(const Point&amp;p) { x = p.x; y = p.y; } void setX(int i) {x = i;} void setY(int j) {y = j;} int getX() {return x;} int getY() {return y;} void print() { cout &lt;&lt; &quot;x = &quot; &lt;&lt; ge
2 min read
Output of C++ Program | Set 12
Predict the output of following C++ programs. Question 1 #include &lt;iostream&gt; using namespace std; int fun(int a, int b = 1, int c =2) { return (a + b + c); } int main() { cout &lt;&lt; fun(12, ,2); return 0; } Output: Compiler Error in function call fun(12, ,2) With default arguments, we cannot skip an argument in the middle. Once an argument
2 min read
Output of C program | Set 25
Predict the output of following C program. int main(void) { struct str { int i: 1; int j: 2; int k: 3; int l: 4; }; struct str s; s.i = 1; s.j = 2; s.k = 5; s.l = 10; printf(&quot; i: %d \n j: %d \n k: %d \n l: %d \n&quot;, s.i, s.j, s.k, s.l); getchar(); return 0; } The above code is non-portable and output is compiler dependent. We get following
2 min read
Article Tags :