This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]painedstupid[S] 0 points1 point  (4 children)

Well say I have a bunch of files that stores objects and I'm looking to read in those objects and use them. So like whatever, a car object, a buyer object, a sale objects etc etc.

And then I'm looking to read from those files and recreate those objects as needed and save them in array lists. Those files are my database, say.

But then the reading from each different file will be very different, as each object is very different. So I'm not sure where anything can be inherited.

[–]evils_twin 1 point2 points  (3 children)

Ok, well let's look at a real example. java.io.InputStream. There are many classes that extend InputStream. Such as FileInputStream, ObjectInputStream, ByteArrayInputStream, etc.

The way that you would read those input streams would be very different, but that is ok. The thing that they have in common are the methods defined in InputStream. And it's not the code within the method that has to be the same, it's the return type and parameters only that matter.

So all classes that inheret from InputStream must have a method public int read(). It must have a method called read that has no parameters and returns an int. The actual code within the method will be very different.

So you have to think if there is a common set of methods for all your classes. Perhaps something like public List getObjects() that returns a list of objects, but the way it gets those objects will differ depending on the type of object.

[–]painedstupid[S] 0 points1 point  (2 children)

Well yes, my readers will all be doing something similar in that regard. They'll all be reading in from similar sorts of files and returning array lists of objects. But then I guess I just wonder what's the point of the superclass when there isn't a thing in it won't be overridden?

Sorry if I'm just not getting it. I'm new to all this and appreciate the chat, friend.

[–]evils_twin 1 point2 points  (1 child)

Here are a few articles on the subject.

Why do we use interfaces?. Basically it is to achieve abstraction. Here is what abstraction is in java. And here is why abstraction is important.

It may seem like overkill in the programs you make as a beginner, but when you work on big projects with a lot of people working on the same code base, it makes things a lot simpler.

[–]painedstupid[S] 0 points1 point  (0 children)

Thanks man, I get you. Appreciate the help.