why inheritance not working with if statement

polymorphism
inheritance in python
inheritance in java
inheritance in c++
multiple inheritance
class inheritance
types of inheritance in python
javascript class inheritance

I have to calculate the student cost base on a different kind of student. There are two types of student, general student, and dorm student. The dorm student will have extra information compared to the normal student. I have two code, one is work, and the other is not. I don't know why it is not working.

I have two class one is Student class and DormStudent class which is the child class (Student class).

This code is not working (below is code 1)

    public static void main(String[] args) {
    String selectType = "Are you Dorm (all other interger) or Communiting (1):";
    int inputSelection=checkInputInteger(selectType);
    String inputName = checkInput("Enter your name:");
    int inputYear = checkInputInteger("Enter Year of Entrance:");
    double inputSuppliesCost = checkInputDouble("Enter Supplier cost per year:");
    int inputCreditHour = checkInputInteger("Enter class hour per year:");
    double inputHourPrice= checkInputDouble("Enter the cost class per hour:");


    if(inputSelection==1){
        int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
        double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
        double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
        DormStudent student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);

    }else{
        Student student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);

    }
    student1.printTotalCost();
}

I have the error say: Cannot find symbol student1.printTotalCost() (student1) but when I try this is work (below is code 2).

public static void main(String[] args) {
    String selectType = "Are you Dorm (all other interger) or Communiting (1):";
    int inputSelection=checkInputInteger(selectType);
    String inputName = checkInput("Enter your name:");
    int inputYear = checkInputInteger("Enter Year of Entrance:");
    double inputSuppliesCost = checkInputDouble("Enter Supplier cost per year:");
    int inputCreditHour = checkInputInteger("Enter class hour per year:");
    double inputHourPrice= checkInputDouble("Enter the cost class per hour:");


    if(inputSelection==1){
        int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
        double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
        double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
        DormStudent student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);
        student1.printTotalCost();
    }else{
        Student student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);
        student1.printTotalCost();
    }
}

I just could not understand why the student1 is not recognize after the if statement in code 1. Is this mean the class initialize only work within the if statement?

You code is basically this:

if(...){
    DormStudent student1 = new DormStudent();
}else{
    Student student1 = new Student();
}
student1.printTotalCost();

student1 is declared in both block. Each block has its own scope. And variable declared inside those blocks are not accessible outside.

You issue is not the printTotalCost method that's not found, it's student1 on the last line which can't be found.

Quick fix: declare the student before the if block:

Student student1;
if(inputSelection==1){
    int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
    double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
    double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
    student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);
    student1.printTotalCost();
}else{
    student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);
    student1.printTotalCost(); // do you really need to print it twice?
}
student1.printTotalCost();

Inheritance — What your mother never told you, C++ FAQ, The only remaining issues are determining Seriously, if there are only one or two places that be passed into the Derived ctor and then up to the Base ctor:. If else statements not working- why? Ask Question Asked 6 years, 9 months ago. Active 6 years, 9 months ago. Viewed 35k times 2. After working on this code for a

You must declare, above the if statement, a single student1 variable with the more abstract type (here, Student I presume) and assign it in each branch with the actual object.

  Student student1;
  if(inputSelection==1){
      int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
      double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
      double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
      student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);
      student1.printTotalCost();
  }else{
      student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);
      student1.printTotalCost();
  }
  student1.printTotalCost();

Class inheritance, Class inheritance is a way for one class to extend another class. So, if a method is not found in Rabbit.prototype , JavaScript takes it from But if we specify our own method in Rabbit , such as stop() then it will be used instead: For the Rabbit constructor to work, it needs to call super() before using this� I may have misled you on the sharing. The share permissions are set much further up the tree and appear to be fine. The issue seems to be purely with UserA. When that user creates or moves an object into FolderA inheritance isn't working. Testing shows that other users in say GroupB do not have this problem.

I see you're having some issues understanding scope. A variable has a certain scope, which means that you can only access it from within the scope it has been declared.

You can find the scope like this: look at the first { that is above the variable, and find the matching } for that one. That is the scope for your variable.

public class App { // start scope text
    public void print(){
        System.out.println(text);
    }
    String text = "hey";
}//end scope text right before the }

It is slightly different for local variables: variables that are declared within methods. These only exists from where they are created, until the } for the first { one on top.

So for example:

if(x < 5) {
    String s = "Hi"; //scope String s starts here
    System.out.println(s);

} //scope s ends just before this }

//if you call s here it no longer exists

And a more complex example:

public class App {//start scope text

    public void print(){
        System.out.println(text);
        int x = 5; //start scope int x
        if(x < 10){
            int y =23;//start scope y
            System.out.println(y + x + text);
        }//end scope y right before the }
    } //end scope int x right before the }
    String text = "hey";
}//end scope text right before the {

Python Tutorial: Inheritance, Python not only supports inheritance but multiple inheritance as well. + self. name) def needs_a_doctor(self): if self.health_level < 0.8: return True else: return False class This doesn't work in Python, as we can see in the following example. Nevertheless, that does not explain why FALSE is returned when you say that F2 is "yes". I suspect it is not. I suspect there are spaces before or after. For example, F2 might be "yes " (space after y-e-s). Confirm with the formula =LEN(F2). It probably returns more than 3.

Inheritance in Java Programming with examples, If you are finding it difficult to understand what is class and object then refer the properties and work() method from base class, we need not to declare these� Inheritance becomes more complicated if the deceased did not outline asset distribution before death. In that case, a probate court must determine the wishes of the deceased as best it can. The probate court will check to see if the deceased named beneficiaries on stocks, bank accounts, brokerage accounts and retirement plans.

Does Java support Multiple inheritance?, For example: Class C extends class A and B then this type of inheritance is known Wait!! here the problem comes – Because D is extending both B & C so if D� Excel’s IF function intimidates a lot of people, but it’s really not that conceptually difficult. The IF function determines whether a statement is true or false, and then takes one of two actions depending on the answer. Let’s look at a plain-English example. Suppose that when the sum of C2 and D2 is greater than …

Inheritance (The Java™ Tutorials > Learning the Java Language , Constructors are not members, so they are not inherited by subclasses, but the constructor of the Except for the constructor, it is as if you had written a new MountainBike class entirely from However, you didn't have to do all the work. then obj is both an Object and a MountainBike (until such time as obj is assigned � If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code. Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero.

Comments
  • what on earth does a condition have to do with inheritance? they are unrelated topics. Your error means that one of your classes does not provide a printTotalCost() method. This has nothing to do with your if statement.
  • This has nothing to do with inheritance. You are trying to use student1 out of the 2 scopes (both the if and else case define it) in which it would be possible. It is not the method that cannot be found but the student1 variable.
  • And, just to say: that first block won't even compile, so it's no wonder it won't work.
  • student1 is in the if scope, it doesn't exist outside.
  • sorry but it not work because student1 = new DormStudent or student1 = new Student is not working. the error variable student1 is already defined in method main(String[])
  • Don't simply add the Student student1;line. You also need to remove the declaration of sstudent1 in each blocks. Note that in my answer, I change both blocks content. I suggest you have a look on a more basic java tutorial before going further into inheritance concepts.
  • Sorry thank you is my fault is work now. I guess I have to redo some of the chapter and go back to inheritance next few days