Java Inheritance Example

Below is an example for Inheritance

class Parent {
    Parent(int a, int b) {
        int c = a + b;
        System.out.println("Sum=" + c);
    }
    void display() {
        System.out.println("Return Statement");
    }
}
class Child extends Parent {
    Child(int a, int b) {
        int c = a - b;
        System.out.println("Difference=" + c);
    }
}
public class InheritanceExample {
    public static void main(String args[]) {
        Child c = new Child(2, 1);
        c.display();
    }
}

I get the following error if I do not have a non-parameterized parent () constructor

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor

    at Child.<init>(InheritanceExample.java:14)
    at InheritanceExample.main(InheritanceExample.java:22)

Could you explain to me what is the purpose of the constructor without parameters in the base class.

+3
source share
6 answers
class child extends parent
{
    child(int a,int b)
    {
        int c=a-b;
        System.out.println("Difference="+c);
    }
}

The first thing a constructor of a child class must do is call the constructor of the parent class. If you do not explicitly do this (for example super(a,b)), the default constructor ( super()) call is implied .

For this to work, you must have this default constructor (without any parameters).

, . , , .

, , , super() , .

:

  • , (super(a,b))

, .

+8

, parent(), , , super (a, b) . , javac () , .

:

class parent
{
   public parent() {
      System.out.println("Parent Constructor");
   }

   public parent(int a,int b) {
      int c=a+b;
      System.out.println("Sum="+c);
   }

   public void display() {
      System.out.println("Return Statement");
   }
}

class child extends parent
{
   public child(int a,int b) {
      int c=a-b;
      System.out.println("Difference="+c);
   }
}

public class InheritanceExample
{
   public static void main(String args[]) {
      child c=new child(2,1);
      c.display();
   }
}

:

Parent Constructor
Difference=1
Return Statement

, :

class parent
{
   public parent(int a,int b) {
      int c=a+b;
      System.out.println("Sum="+c);
   }

   public void display() {
      System.out.println("Return Statement");
   }
}

class child extends parent
{
   public child(int a,int b) {
      super(a,b);
      int c=a-b;
      System.out.println("Difference="+c);
   }
}

public class InheritanceExample
{
   public static void main(String args[]) {
      child c=new child(2,1);
      c.display();
   }
}

:

Sum=3
Difference=1
Return Statement
+3

, :

?

: "" "parent" , "child" "parent" (), "". , , "", "", "parent" super() . , "" .

0

The error is that if we do not call super explicitly, then the JVM puts super () in the constructor of the child class, and this super () looks for the constructor in the parent class without a parameter that is not in your class so this is wrong. Either put the non-parameterized constructor in the parent class, or put the super (a, b) operator in the very first line of the child constructor.

 class Parent 
    {
        Parent(int a, int b) 
       {
            int c = a + b;
            System.out.println("Sum=" + c);
           }
        void display() 
    {
         System.out.println("Return Statement");
        }
    }
    class Child extends Parent 
    {
        Child(int a, int b) 
    {
        super(a,b);
        int c = a - b;
             System.out.println("Difference=" + c);
       }
    }
    class InheritanceExample 
    {
        public static void main(String args[]) 
    {
             Child c = new Child(2, 1);
        c.display();
       }
   }
0
source
public class Mobile{
private String manufacturer;
private String operating_system;
public String model;
private int cost;

Mobile(String man, String o,String m, int c){
this.manufacturer=man;
this.operating_system=o;
this.model=m;
this.cost=c;
}
public String getModel(){
return this.model;
}
}

public class Android extends Mobile{
Android(String man, String o,String m, int c){
super(man,o,m,c);
}
public String getModel(){
return "This is Android mobile" +model;
}
0
source
import java.io.*;
public class XXX
{
public static void main()throws IOException
{
System.out.println("Enter your name.");
String name = in.readLine();
System.out.println(name+" rules!! Thank You!!");
}
}
0
source

All Articles