+ 2
single Inheritance... please find out the Error... I tried a lot... Thank you..
class A { int a=5; } class C extends A { int b=6; void add() { System.out.println("The addition is "+(a+b)); } } Class Inter { public static void main(String arg[]) { C obj= new C(); obj.add(); } }
4 Antworten
+ 2
Make it 'class Inter', you will get answer as 11. The compiler was not finding valid identifier. Java is case sensitive.
+ 1
it gives me no problem, I ran it in eclipse and it gives me 11 as result...
0
Holy Shit....I didn't see that... it was giving me 5 errors and I was like what the fuck is wrong with this small program.... Thank you Apoorva...
0
the default access modifier says that if you didn't add access specifier to a variable then it can be accessible in subclass but should be in same package and it is inaccessible in the other package class.
considering your question if you define class A in different package and define class B in another package then the variables without access specifiers not accessible in subclass.
to make your code work you have to explicitly specify access specifier to your variable say.
protected int a = 5;
or
public int a = 5;