0

Hi Everyone - I am newbie in developer community, I am working on inheritance where I am trying to call the variable of Master class intochild one but I am afriad it is not working out. Any help will be appreciable.

//Master Class

public virtual class Area 
{
    Public Integer Area;
    Public Integer Rate;
    Public Integer PropertyPrice;

    Public virtual void Areacalculation()
    {
        PropertyPrice = (Area*Rate);
        System.debug('Total Area is' +Area);
        System.debug('Rate of per square foot is' +Rate);
        System.debug('Price of the deal is' +PropertyPrice);
    }
}

//Child Class

public class PropertyCommision extends Area
{
    Public Integer ComCalc;

    Public void ComCalculation()
    {
        ComCalc = PropertyPrice*2/100;
        System.debug('Commision of property is ' + ComCalc);
    }
}

//Debug Statment run in compiler

Area A = New Area();
A.Area = 1500;
A.Rate = 3500;
A.Areacalculation();

Area A2 = New PropertyCommision();
A2.Areacalculation();
A2.ComCalculation();
2
  • Please describe what you wish to accomplish, in addition to how you're currently attempting to go about it. Mar 10 '21 at 1:55
  • If you're encountering a specific error, please edit to add the complete text to your post.
    – David Reed
    Mar 10 '21 at 2:06
0

A2.ComCalculation(); won't work, because the compiler only knows that you have an Area in A2. All PropertyCommision are an Area, but not all Area are a PropertyCommision, which is why you can't call that method; the method doesn't exist as an Area. Instead, you can define A2 as a PropertyCommision.

Area A = New Area();
A.Area = 1500;
A.Rate = 3500;
A.Areacalculation();

PropertyCommision A2 = New PropertyCommision();
A2.Areacalculation();
A2.ComCalculation();

There are reasons why you'd want to put a PropertyCommision into an Area variable (perhaps because you're calculating a bunch of items and you don't care what type they are), but in this case, it doesn't help you.

You can read more about a use case for a top-level class in this answer (which deals with interfaces, but the concepts are similar), and this answer (abstract and inheritance), and possibly also this answer. We probably have some more laying around, but I hope this gets you started.

0

Inheritance doesn't work in the way your example is showing (two separate object instances trying to share data with one another). I'd argue that inheritance isn't the correct solution for this exercise, but for sake of learning let's set that aside and continue.

Inheritance has a few different uses. The one I think you're trying to explore here is to help in code re-use. Instead of having to repeat yourself multiple times, you place common variables and methods into a parent class, and then have child class(es) inherit those.

Inheritance is what is allowing you to use the PropertyPrice variable in your PropertyCommision class even though PropertyCommision itself doesn't contain the PropertyPrice variable. That right there is most of what inheritance provides us (another big part being that an instance of the child class can be treated as if it were an instance of the parent class).

Making your example work (by which I mean not crash, and produce the output I think you're looking for) would look like

// The "Area" class doesn't have a ComCalculation() method
// If you want to call that method, you need to keep your PropertyCommission instance
//   typed as a PropertyCommission
PropertyCommission a2 = new PropertyCommision();

// Inheritance doesn't let you share data between object instances
// You'll need to set the class variables on the a2 instance
// You don't define the area and rate class variables in PropertyCommission, but you
//   did define them in Area.
// Inheritance is what allows you to reference these variables even though you're working
//   with a PropertyCommission
a2.area = 1500;
a2.rate = 3500;

// Likewise, areaCalculation() isn't defined in PropertyCommission
// Inheritance is what allows you to call that method
a2.Areacalculation();
a2.ComCalculation();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.