Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I'm trying to use inheritance and polymorphism but when I try to make a derived class parallelogram from a Quadrilaterall base class I get a constructor error inaccessibility error.
The code is below:

namespace InheritenceANDPolymorphism
{
   public  class Quadrilateral
    {
        private double s1;
        private double s2;
        private double s3;
        private double s4;
        Quadrilateral(double side1, double side2, double side3, double side4)
        {
            this.s1 = side1;
            this.s2 = side2;
            this.s3 = side3;
            this.s4 = side4;
        }
        public double getPerimeter()
        {
            double result = s1 + s2 + s3 + s4;
            return result;
        }
        public virtual double getArea()      // pure virtual - this makes the class abstract
                                      // forcing use of this class through inheritance
        {
            double result = s1 * s2;
            return result;
        }
    }
}


class Parallelogram : Quadrilateral
   {
       private double height;
      // public virtual void Parallelogram(double s13, double s24, double h)
      // {
      // }
       Parallelogram() //I get the error here...
       {
       }


Any suggestions?
Posted
Updated 28-Apr-11 16:24pm
v2
Comments
walterhevedeich 28-Apr-11 22:27pm    
It would help if you post the exact error message.

Make Quadrilateral.Quadrilateral public (accessible from anywhere), protected (accessible from derived classes), internal (accessible from anywhere from the same assembly) or internal protected (accessible from derived classes in the same assembly), but not private (as you did). So, there are five variants of access modifiers; and only one would cause and error; you've chosen the only one which does; this is probably because it is a default — you did not use any access specifiers. You should remember them all and develop a habit to use minimal required access in all cases.

Now, why construction of a derived class requires access to a constructor of a base class? Because it is called implicitly.

—SA
 
Share this answer
 
v2
Comments
Tarakeshwar Reddy 28-Apr-11 22:50pm    
My 5 for the detailed explanation. Will try to be detailed as you in the future :)
Sergey Alexandrovich Kryukov 28-Apr-11 22:52pm    
Great. Thank you, Tarakeshwar. (Maybe I over-detail but in this case: if you say A, say B... :-)
--SA
Albin Abel 28-Apr-11 23:41pm    
Good, this makes OP think a little. my 5
Sergey Alexandrovich Kryukov 28-Apr-11 23:59pm    
Thank you, Albin.
--SA
The constructor for quadilateral should be public/protected. When you do not specify an access modifier it is private by default.

The other issue is, you need to call the base constructor and pass the arguments to it from the parallelogram class.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Apr-11 22:47pm    
You spotted the bug, but your explanation is incomplete; there are five access specifier, four of them would work (not two as you write). My 4.

Please see my answer of a full list.
--SA
Albin Abel 28-Apr-11 23:42pm    
My 5. You spot the problem
Tarakeshwar Reddy 29-Apr-11 0:12am    
Thank you!
Hey there,

Yes, you need to add "public" to the class definition and the constructor definition of the class "Parallelogram". (It cannot be marked "Protected" since .NET throw an error saying the inherited class is less accessible than the base class). In addition to what they mentioned, please take note of below points as well.

The class "Quadrilateral" has an overridden constructor which removes the default constructor of a class. If you want the default constructor back, you need to explicitly define it again.

The class "Parallelogram" inherits from the "Quadrilateral" and has only a default constructor (i.e. no arguments). What happens here is when you create an object of "Parallelogram", .NET looks for the constructor with the same number of arguments in the base class, hence would throw an error to you. You can overcome this by changing the code to something similar below, or changing the constructor of "Parallelogram" to have similar arguments and call the base constructor.

C#
public Parallelogram() : base(0, 0, 0, 0)
{
}

Or
C#
public Parallelogram(double side1, double side2, double side3, double side4) : base(side1, side2, side3, side4)
{
}

public Parallelogram() : this(0, 0, 0, 0)
{
}


Hope this helps you :) Regards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900