The use of an abstract class and method: Now, suppose you have a parent Animal class. And two children, Cat and Dog, extending Animal. We may want to create Cat and Dog objects, but we almost will never be creating any Animal objects. We only have the Animal class for the children to inherit from it. Since there's no use for instantiating an Animal class on its own, we make it abstract. And an abstract class cannot be instantiated in the main() function. Now let's see abstract methods.. A Cat and a Dog may have the same function named "makeNoise()", but both of them make different noises. So an abstract method is defined in the abstract class, and we override that method in the child classes. I hope this helps. :)
Say for example you are making an app ,and their are many teams for that .You are making the design that each page in the app must have several features mandatory like title , chat bot , side bar so you will make an abstract class and will define abstract methods like title, sidebar, chatbot . Each team, who works on different pages of the app must inherit this class and while doing this they are forced to obey these features. That is abstract methods it compulsory that you maut add that methos in all inheriting classes
@@kaiparado Say for example you are making an app ,and their are many teams for that .You are making the design that each page in the app must have several features mandatory like title , chat bot , side bar so you will make an abstract class and will define abstract methods like title, sidebar, chatbot . Each team, who works on different pages of the app must inherit this class and while doing this they are forced to obey these features. That is abstract methods it compulsory that you maut add that methos in all inheriting classes
Sir as you said , after inheriting the abstract class and its method , we can use another method in abstract class with body but what is the purpose of using a method with body in an abstract class when it can not be instantiated ?
actually you have to call that method with the object of child class. like a method eat can have body in abstract class of animal so we can call that eat method by object of child class like cat.eat();
I will recommend you just remember you can't instantiate an object of an abstract class, and search about the difference between abstract class and interface.
It is helpful if you need to make a generic function that can take a lot of class types as an argument. Eg: abstract class Shape { void area(); void perimeter(); } class Rectangle extends Shape { int width; int height; Rectangle(this.width, this.height); void area() => this.width * this.height; void perimeter() => 2*(this.width + this.height); } class Triangle extends Shape { int side1; int side2; int side3; Triangle(this.side1, this.side2, this.side3); void area() => 0.5 * this.side1 * this.side2 * this.side3; void perimeter() => this.side1 + this.side2 + this.side3; } void printGeometry(Shape shape) { print("The area of this shape is ${shape.area()}";) print("The perimeter of this shape is ${shape.perimeter()}";) }
Abstract Classes are usefull for code organization i.e. Contracts: Abstract Classes One way a contract is implemented is via an abstract class. An abstract class is a class that contains one or more methods that do not have any implementation provided. Suppose that you have an abstract class called Shape. It is abstract because you cannot instantiate it. If you ask someone to draw a shape, the first thing they will most likely ask you is "What kind of shape?" Thus, the concept of a shape is abstract. However, if someone asks you to draw a circle, this does not pose quite the same problem because a circle is a concrete concept. You know what a circle looks like. You also know how to draw other shapes, such as rectangles. How does this apply to a contract? Let's assume that we want to create an application to draw shapes. Our goal is to draw every kind of shape represented in our current design, as well as ones that might be added later. There are two conditions we must adhere to. First, we want all shapes to use the same syntax to draw themselves. For example, we want every shape implemented in our system to contain a method called draw(). Thus, seasoned developers implicitly know that to draw a shape you simply invoke the draw() method, regardless of what the shape happens to be. Theoretically, this reduces the amount of time spent fumbling through manuals and cuts down on syntax errors. Second, remember that it is important that every class be responsible for its own actions. Thus, even though a class is required to provide a method called draw(), that class must provide its own implementation of the code. For example, the classes Circle and Rectangle both have a draw() method; however, the Circle class obviously has code to draw a circle, and as expected, the Rectangle class has code to draw a rectangle. When we ultimately create classes called Circle and Rectangle, which are subclasses of Shape, these classes must implement their own version of Draw
We can make the child class of the abstract parent class.....Suppose you have some properties and methods present in abstract parent class..and you want these properties and methods only usable by child class but not by object of that abstract parent class....that's why we make the abstract class...
You did not mention anything about method declaration. what about overriding an abstract method with different list of arguments??? Is that allowed? Hmm.
its not mandatory to have abstract method in abstract class. then y we use abstract class. you are only telling how to type syntax. but u never show an example or scenario where it can work. try this code with out abstract method in abs, class it works..... void main() { var dog = Dog(); dog.shape(); } abstract class Animal { void shape() { print("hwwwwwwwwwwwwwwwwwww"); } } class Dog extends Animal { void shape(); }
If you are designing large functional units, use an abstract class. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members. LINK: www.codementor.io/vipinc/when-to-use-abstract-classes-and-when-interfaces-c-interview-questions-jtzll1sn7