9.5 Abstract class
Hello everyone, in this video we will talk about abstract classes An abstract class is a class whose all methods have not been implemented. It is therefore not instanciable, but serves primarily to factor code. A class that inherits an abstract class must necessarily implement the missing methods that have themselves been declared «abstract» in the parent class. it is not obliged to reimplemente the methods already implemented in the parent class, which makes code maintenance easier. We will make an example of an abstract class so, we will create a class we go in the file src -> then again the class java and as class name Dog. The class will be abstract and as a string variable. Then we will create a normal method and we will implement it So it will be public void And as the name "Bark" for example And inside the bunker then tab Now, we can try to use this abstract class in our hand by creating the object, It will be Dog d = new Dog And here we have a mistake that tells us we can’t instantiate the Dog class because it’s abstract. So it can’t be instantiated. It is necessary to create a concrete class, that is, not abstract, which extends it, and to instantiate this concrete class. So let’s create a class that will inherit the dog class and as a name "chihuahua". And in dog class, we’ll put our abstract method. Now that our method is abstract, we can’t write anything in it. and this is where we can write something inside. We’ll call the method and inside. We will write for example "Bark" Now, that we have our abstract method in our class "chihuahua" We can now create the chihuahua object in our hand class. So, now we’re going to call our method "Bark" So, it will c.Bark then, we launch the program. to see if it works. And as we can see, it is well written "Bark" That was all for this video, I hope you understood more or less what the abstract class is Go I tell you next.