+ 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(); } }

23rd Jul 2017, 8:31 AM
Subhankar Golder
Subhankar Golder - avatar
4 Antworten
+ 2
Make it 'class Inter', you will get answer as 11. The compiler was not finding valid identifier. Java is case sensitive.
23rd Jul 2017, 8:45 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 1
it gives me no problem, I ran it in eclipse and it gives me 11 as result...
23rd Jul 2017, 8:38 AM
2_3rr0r5
2_3rr0r5 - avatar
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...
23rd Jul 2017, 8:50 AM
Subhankar Golder
Subhankar Golder - avatar
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;
23rd Jul 2017, 8:57 AM
Keshave Jat
Keshave Jat - avatar