inheritence in java inherit variables?
Asked Answered
S

6

6

As I understand the inherited class should also inherit variables, so why doesn't this code work?

public class a {
    private int num;

    public static void main(String[] args) {
        b d = new b();
    }
}

class b extends a {
    public b() {
        num = 5;
        System.out.println(num);
    }
}
Stanch answered 15/2, 2013 at 9:54 Comment(3)
You cannot inherit the private variablesUnusual
@PradeepSimha as a matter of fact you can't inherit just any instance variables. they are only visible if they are marked public/protected/no-modifierCensus
@PremGenError, yes I know that. So what? :)Unusual
C
15

num variable's access modifier is private and private members are not accessible out of own class so make it protected it will accessible from subclass.

public class a {
     protected int num;
     ...
}

Reference of Controlling Access to Members of a Class

Curtin answered 15/2, 2013 at 9:55 Comment(0)
C
2

As i understand the inherited class should inherit also variables,

you got it wrong, instance variables are not overriden in sub-class. inheritence and polymorphism doesnt apply for instance fields. they are only visible in your sub-class if they are marked protected or public. currently you have super class variable marked private. no other class can access it. mark it either protected or public in-order for other class's to access it.

Census answered 15/2, 2013 at 9:56 Comment(0)
H
2
public class A{
    public int num=5;
    public static void main(String[] args) {
        b d = new b();
        d.c();
    }
}

class b extends A
{
    public void c() 
    {
        System.out.println(num);
    }
}

definitely this is what you need i think

Helsa answered 9/3, 2015 at 20:31 Comment(0)
L
1

private scope can only be accessed by the containing class.

For this to work num would need to be declared protected scope.

However this would also make it accessible to other classes in the same package. My recommedation would be to create a get / set method in order to maintain proper encapsulation.

you could then access num in class b by calling getNum()

Lustrum answered 15/2, 2013 at 9:55 Comment(0)
B
1

Because you are using the private access modifier. If you use private to a instance variable or to a method it only can access inside the class only(even several classes include one source file). We can expose private variable to outside by using getters and setters. Following code will compile without an error

public class A {
    private int num;

public void setNum(int num)
{
     this.num = num;
}

public int getNum() 
{
      return num;
    }


    public static void main(String[] args) 
{
        B d = new B();
    }


  }
  class B extends A
       {

      public B()
 {

        SetNum(5);
        System.out.println(getNum());
     }
   }
Bugleweed answered 15/2, 2013 at 11:14 Comment(0)
P
0

You don't have access to private members of the base classes from the subclass. Only the members with modifiers of private/protected

Profile answered 15/2, 2013 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.