• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and abstraction  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I am kind of new here and to java as well. Today I was studying inheritance and polymorphism and such. Everyting was crystal clear but then
when I started tinkering with various bits of code, I realized there are so many things I still don't understand, which cast a shadow of doubt on
all the things I thought I knew. Anyway, I will now post the code and subsequently explain what I don't understand. The project consists of 4 classes,
a main, an abstract parent, and 2 children classes (is that what they are even called in enhlish?).

Parent class:


Pretty straightforward. Now let's inherit this into the other 2 classes.



And second.


and main


Please note that I am aware that the songs, albums and dates are all random. And Fall out Boy are not a pop band.
This being said, first I removed the "this." bit in every single method in every single class. The code still worked properly.
Why is that? Cause when I took it out of the constructors, the code broke. Are they useless outside the constructor?

Anyway, it emerged (this project is intentionally flawed, it was meant to explain some basic concepts) that the getPlaylist
method in the Album class was a bit off. It really is useless because it will never play any role other than making the play()
method work. Now they started explaining that we can have abstract methods, they don't return anything and make the play()
method work. Is this example poorly designed? Because I thought to my self we do not really need that method at all. Just change
the play() method to



and get rid of getPlaylist altogether. Or even


since the "this." bit seems useless anyway.
Any thoughts? I have more questions but yeah, maybe my brain doesn't really get the programming logic.
 
Saloon Keeper
Posts: 3461
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" appears frequently in constructors because often there is a parameter name that is identical to a field of the class and "this" clarifies that you are specifically referring to the class field and not a local variable.

Often people just get into a bad habit of adding "this" when it is unnecessary. Those should be removed.
 
Amadeus Pan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"this" appears frequently because often there is a parameter name that is identical to a field of the class and "this" clarifies that you are specifically referring to the class field and not a local variable.

Often people just get into a bad habit of adding "this" when it is unnecessary. Those should be removed.



Thanks. Yes that makes a lot of sense though I still really don't get when they might be used outside a constructor. Like, if playlist was a parameter of the constructor, then I get it, but otherwise is there a reason to use it?

Another question  have is, after this part, the professor went on to create yet another class which inherited the Album class. He made a getPlaylist method in it that returned a string. Let me post it.


then in the main



When he ran it, in the output there was also the "Kidds songs" string. But when I tried it gave me a null.
I had to write the play() method in this new class as well for it to work. Does that have anything to do with the this. bits I took out?
I tried to put them back in but still got nothing without the play() method in the new class.

Sorry for all these questions, I realize that for more experienced programmers they are silly.
 
Carey Brown
Saloon Keeper
Posts: 3461
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cut-n-pasted your code into a java file and I did not get null, so I'm not sure where the problem was introduced. you should not need a play() method in KidsSongs.

Here's the file I used. Note that you can put multiple classes in a single .java file as long as only one is public and that one matches the file name.
 
Amadeus Pan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes my bad. I changed the play() method in the album class to print this.playlist instead of getPlaylist. Thank you. I am aware of that, it was among the first things we learnd, but I prefer having them in separate files,
it is easier to browse.
 
Amadeus Pan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It kinda sucks when I have these questions torture me all day, then as soon as I send it to someone, I start noticing my dumb mistakes.
 
Carey Brown
Saloon Keeper
Posts: 3461
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ammar Pehilj wrote:Yes my bad. I changed the play() method in the album class to print this.playlist instead of getPlaylist. Thank you. I am aware of that, it was among the first things we learnd, but I prefer having them in separate files,
it is easier to browse.


I wasn't necessarily advocating that you put it all in one file. When you ask for help in forums it is often easier to help you if there's only a single file to cut-n-paste and if you tweak one class then all the code stays together. When you post multiple changes to various bits and pieces we're no longer sure they're in sync.
 
Amadeus Pan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
I wasn't necessarily advocating that you put it all in one file. When you ask for help in forums it is often easier to help you if there's only a single file to cut-n-paste and if you tweak one class then all the code stays together. When you post multiple changes to various bits and pieces we're no longer sure they're in sync.



Ah that makes sense, I will keep that in mind in my future posts!
 
Bartender
Posts: 756
25
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ammar Pehilj wrote:I still really don't get when they might be used outside a constructor. Like, if playlist was a parameter of the constructor, then I get it, but otherwise is there a reason to use it?

Outside of the constructor keyword this can be used in mutator/setter method (Used to set values of private variables), reason of use is same as you used in your constructors i.e. having parameters and class instance field with same name. Example
 
Sheriff
Posts: 4432
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ammar Pehilj wrote:It kinda sucks when I have these questions torture me all day, then as soon as I send it to someone, I start noticing my dumb mistakes.


It does, but this happened not only to you, but others as well.  I think all of us have experienced something like this one or more times.
 
Amadeus Pan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote: Outside of the constructor keyword this can be used in mutator/setter method (Used to set values of private variables), reason of use is same as you used in your constructors i.e. having parameters and class instance field with same name. Example



That is a very clear and enlightening explanation! Thanks a lot! This forum is proving to be a real asset.
 
Ganesh Patekar
Bartender
Posts: 756
25
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for those kind words. It's a pleasure.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!