Example: A dog has states - color, name, breed as well as behaviors − wagging the tail, barking, eating. Object − Objects have states and behaviors. In this chapter, we will look into the concepts - Classes and Objects. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts − However, if your class is abstract, it may have some methods that are abstract and others that are concrete.Java is an Object-Oriented Language. Otherwise users could instantiate the class and try to implement its methods. If you want an abstract method, the class must also be abstract. You can’t have an abstract method in a concrete class. If you want to use the parent class’s method instead, you use the super keyword, like this: If the subclass has a method with the same name as the parent’s method that the subclass extends, the subclass’ method overwrites the parent. not need to implement their own versions of it All descendant classes can call this and do And here’s a method for making the sound. Listing 6-1 shows both kinds of methods within the Mammal class. So, in this case, the implementations must return void and accept no arguments.Ībstract classes can also include regular methods that their descendant classes can use without needing to implement them. Any class that extends Mammal must implement the speak method, and that implementation must have the same signature. For example, our Mammal class includes an abstract speak() method. Any class that extends a class with an abstract method must implement that method. Modeling the animal kingdom is a simple example, so it’s not hard to see that Mammal should be an abstract class.Ībstract classes can include abstract methods. The hard part is figuring out when a class should be abstract. Returning to our example, we can have Mammal be abstract (because there’s no such thing as a generic mammal) and still have Cat, Dog, and Mouse extend Mammal (because cats, dogs, and mice are mammals). In Java, abstract means that the class can still be extended by other classes but that it can never be instantiated (turned into an object). Java lets developers declare that a class should never have an instance by using the abstract keyword. Consequently, we don’t want anyone to create an instance of our Mammal class, because that would be bad modeling. So, there’s no actual animal that’s a generic mammal. Cats, dogs, mice, humans, whales, and so on are all mammals, of course, but they all have more specific names and more specific traits. All the mammals in the real world are specific types of mammals rather than just mammals. Our Mammal class is an example of such a class. Such a class usually serves as the basis for other classes. That is, it’s a class that will never have an object associated with it. Sometimes, a Java developer writes a class that should never be instantiated. I think you only use abstract classes if you want to provide a utility that people can use to perform some action, but not which they can modify or somehow instantiate and use in a different way. If you make a class abstract, you can’t instantiate an object from it. Some other object must instantiate the item in question. Abstract classesĪbstract (which Java supports with abstract keyword) means that the class or method or field or whatever cannot be instantiated (that is, created) where it is defined. When you use a class to extend another class, you aren’t required to implement all of the methods. The reason you don’t use an interface is because interfaces require you to implement all of the methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |