• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does inheritance work with abstract classes  RSS feed

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing small pieces of code to make sure I understand Java basics and I have the following.



It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1. Any help would be appreciated. Thanks!
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the Java Language Specification (JLS), it tells you that private members are not inherited.
So the String field you showed is not inherited.
But why are you initialising it with a constant value? Why not initialise it via the constructor? If you do so, passing a parameter, you can initialise it via a super() call in the subclass's constructor.
Get rid of that set method. The get method should simply return the field. It is dangerous to return mutable reference types like that, unless you take a copy, but that problem doesn't apply with Strings.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I wasn't trying to do anything in-particular with the code. I was just experimenting with writing abstract classes to see how they work. Thanks again for the info.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:Thanks for the help. I wasn't trying to do anything in-particular with the code. I was just experimenting with writing abstract classes to see how they work.

And that's just fine. In fact, I'd say: do as much of it as possible.

In answer to your general question: it (inheritance) works pretty much the same as it does everywhere else, except that abstract classes are allowed to define abstract methods, which must be implemented before a subclass can be instantiated. Otherwise, they're pretty much like a normal class - including the rules that Campbell outlined for you.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . do as much of it as possible. . . .
Agree. The more “I just want to see what happens if …” code you write the better. But tell us it is “I just want to see what happens if …” code. Otherwise we shall think it is real code and get all worried about it!
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will do that. Thanks!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its good to try something new and ask ourselves "why this" . In the above code, you have declared the variable as a private !. by making it private, you cannot access that variable outside your class , and that variable you have declared in abstract class,and you need to implement all the entities in the concrete class down the tree, so it would be good if you try with access specifiers "protected" and default" to know the difference !! keep trying for all the options you have, to enjoy the java !!! good luck.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
datta Nadgir wrote: . . . try with access specifiers "protected" and default" . . .
… and so you find why that is probably bad design.

They are called access modifiers not specifiers. And you don't write default; you simply leave out the modifier.
 
Greenhorn
Posts: 18
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We were having a class today on access modifiers and it was stated that even if the superclass declares a private that you cannot access it directly from an instance of any direct or indirect subclass but if there are any public methods like getters or setters in the parent class that access or alter that private instance variable, then instances of the subclass will be able to manipulate it indirectly.

We can indirectly access it via getters and setters. Using statements such as SameColourAs (), getColour () and setColour(). We were told to be very careful with what access modifiers we use as they support data hiding and prevent accidental and intential misuse of private members of a class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!