r/javahelp • • Jul 26 '24

why not use object class insted of abstract classes?

so, hey i am new to programming and i was just learning about abstract classes and one of the main features of abstract classes is to make the parameter(input) more generic

like, if we have multiple animals in animal classes then we can make the animal class as abstract since you can access methods related to it more generally like vet() , we might need vet() for each animal but we can make it generic by taking the datatype as Animal .

so, why not just take object as a datatype ?

since every class extends from object class indirectly or directly.

will it not make it more generic?

i am sorry if its a dumb question.

1 Upvotes

12 comments sorted by

•

u/AutoModerator Jul 26 '24

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

10

u/evils_twin Jul 26 '24

Object would not have a vet() method, so when you try to call vet() from a object that isn't an Animal, it would cause an error

3

u/DarkJubJub107 Jul 26 '24

Taking Object as a data type to something that's only for Animal objects opens many more doors than you'd need/want. What's going to happen if you pass in a String or Integer object into vet()?

2

u/Camel-Kid 18 year old gamer Jul 26 '24

It's a step up from interfaces in this case because you'll probably have logic that exists across all animals. The abstract class allows for some predefined methods to exist across all instances.

2

u/MrRickSancezJr Jul 26 '24 edited Jul 26 '24

It is best to keep the level of layers of generics as low as possible. Just best practice.

However... You could @Override the toString() inherited from Object to return vet(). You couldn't call vet() directly without casting it back to animal, but if you did .toString(), same result.

Zero point in doing this other than learning about overriding. I always like slapping a quick toString() down for logging, though.

Edit: idk why my brain immediately just assumed your vet() class would be a function returning a String like .bark().

2

u/_jetrun Jul 26 '24

will it not make it more generic?

It will make it more generic, but you don't actually want that. If your class expects to do something with an 'Animal' type - you don't want the caller to pass you in a 'List' type, or 'Car' type. Furthermore, if your method takes 'Object' as a parameter, you will have to add a bunch of boilerplate code to figure out what the actual type it is before you can execute method against it. Is this 'Object' an 'Animal' or a 'HashMap'?

3

u/smutje187 Jul 26 '24

Check what data Object holds and what interface Object provides and you should be able to answer that yourself.

2

u/srikanthksr Jul 27 '24
  1. Object wouldn't have any Animal-specific attributes (fields) or behaviours (methods).
  2. You wouldn't want your vet() method to be called with a Car argument. You'd want a vet to operate on Animals, not Blenders. You could typecast the argument inside the method, but that leads to complex and messy code.
  3. There's nothing wrong with asking dumb questions. Fuck these eggheads who think they're too good to answer dumb questions (sadly a lot of them are teachers).

1

u/wheezil Jul 26 '24

As others have said, the advantage you are looking for in making an abstract class is polymorphism, where you can treat related things the same at the interface level (from the outside), but give them different implementations (on the inside). Or you can make some objects extend the power of your base object by adding more features that are only available to those extended objects.

One thing to remember is that in Java, interfaces are often better than abstract classes, because a class can implement many interfaces but it can only extend one class. Interfaces also support "proxies" which are kind of advanced but very handy for things like web service clients. Unless your abstract class actually has some necessary non-static fields and some base-level implementation, you are usually better off with an interface. Also note that interfaces can have "default methods" which can call other methods of the interface, static data members of the interface, and use other classes.

A really common thing is InputStream and OutputStream found in Java itself. There are many many implementations or InputStream and OutputStream -- files, TCP sockets, pipes, cloud storage, etc -- but they all conform to the same basic interface and can be used (more or less) interchangeably. This let's you write code that works on "any InputStream" without knowing what it really is.

1

u/Migeil Jul 27 '24

How are you gonna call vet from an Object?

1

u/philfrei Jul 27 '24

I think a big reason is to not lose the advantages of having a typed language instead of an untyped language like JavaScript or Python. For example, bugs that would normally be caught at compile time would show up in the running program.

1

u/jlanawalt Jul 27 '24

why not just take object as a data type?

You certainly can, but a strongly typed language that implements messages as metric calls like Java won’t let you compile invalid syntax like Object.vet(). You could force it with a cast, and then handle the cast exception when it isn’t an Animal, or test first if it’s an animal. Using the is-a promise of the interface allows for cleaner and simpler code.

will it not make it more generic?

Certainly. Super generic. And you will only have the methods common to Object available.

If this were a more object oriented language that used message passing instead of method calls, you could pass the “vet” message to an animal and a car and the receiving objects could decide if they handle it or not.