• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question on inheritance

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone

I have a few quesions in the following code

class A
{
int i=20;
}

class B extends A
{
private int i=99;
}
class C extends B
{}

class test99
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
System.out.println(a.i);
//System.out.println(b.i);
System.out.println(c.i);
}
}

1)Since B extends A,it already 'has' int i.So why is it allowed to redeclare it and make it private.
2)Why doesnt c.i compile?Though i is private in B,it has default access in A, and C is sub class of A indirectly.Why doesnt this compile?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi praneeth,

have a look at 8.3.3.2 Example: Hiding of Instance Variables in the Java Language Specification.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praneeth,

Pls read the notes on inheritance.
We have to learn the behavior as Rules. just apply the rules.


class A
{
int i=20;
}

class B extends A
{
private int i=99;
}
class C extends B
{}

class test99
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
System.out.println(a.i);
//System.out.println(b.i);
System.out.println(c.i);
}
}

1)Since B extends A,it already 'has' int i.So why is it allowed to redeclare it and make it private.
2)Why doesnt c.i compile?Though i is private in B,it has default access in A, and C is sub class of A indirectly.Why doesnt this compile?
[/QB]




it is very simple.
1) redeclaration is allowed in inheritance, now B.i is different from A.i, Programmer can prevent a variable access in sub class by specifying it as private though it is declared in super class.
2) c.i is won't compile. because the immediate super class B is having private access to i, then its subclass cannot access it.


Thanks
Madhu
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am totally agree with the "madhu v pe". at first i also get same question what you get.. but after reading posted reply my question get clear...
 
Pay attention! Tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic