Does a class inherit the constructors of its superclass

A class does not inherit constructors from any of its superclasses.

Showing Answers 1 - 24 of 24 Answers

I think yes. there are two ways to access the super class constructors they are :


First Method:

class A
  {
    A()
          {
          System.out.println(" First Message ");  
       }
      
   }
  
   class B extends A
  {
 
     B()
       {
       System.out.println(" Second Message ");
       }
   
  public static void main(String a[])
    {
   
       B b=new B();
    }
 }  

Second Method:Thr Super

class A
  {
   double wid;
   double hei;
   double dep;

     A(double w,double h,double d)
      {
    wid=w;
    hei=h;
    dep=d;
   }


   }

class B extends A
  {
double wei;

     B(double w,double h,double d,double m)
      {
    super(w,h,d);
    wei =m;


   }

   double vol()
       {
        return wid*hei*dep*wei;
    }


   }
class C
 {
  public static void main(String a[])
    {

        B b=new B(2.0, 2.0, 2.0,2.0);

        System.out.println(" >>>>>  " +b.vol());

    }
 } 

  Was this answer useful?  Yes

No it does not inherit the constructor of the superclass. however when you create a object of subclass the constructor of the superclass is called first and then the constructor of sub class is called

  Was this answer useful?  Yes

A super default constructor is automatically available to the subclass , when the subclass object is created. but if you want to access super class parameterized constructors , we must use super keyword.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions