Is it mandatory to implement all the methods which are there in abstract class if we inherit that abstract class..?

Showing Answers 1 - 7 of 7 Answers

VVBABU

  • Jul 24th, 2005
 

Question : Is it mandatory to implement all the methods which are there in abstract class if we inherit that abstract class..? 
 
A: Yes I it is Mandatory

  Was this answer useful?  Yes

Saroman

  • Oct 31st, 2005
 

No only function marked as abstract shoud be implemented.the following code runs fine abstract public class baseclass { public abstract void method1(); public void method2(){Console.WriteLine("Base Method 2");} } public class derivedclass:baseclass { public override void method1(){Console.WriteLine("Derived Method 1");} //public override void method2(){Console.WriteLine("Derived Method 1");;} }

  Was this answer useful?  Yes

s

  • Feb 15th, 2006
 

No...not necessarily.

if we declare even the inherited class as "abstract" , then, tht abstract method of parent class need not be implemented.

  Was this answer useful?  Yes

learnasp

  • Oct 11th, 2007
 

If the inheriting class(child class) is not abstract then we need to override (implement) all the abstract methods otherwise it will give compile error.

To keep methods abstract in the inheriting class the inheriting class should be abstract too.

  Was this answer useful?  Yes

NO


Even you need not to implement ALL of the PURE VIRTUAL FUNCTIONS of Abstract Class, in Derived Class.
But then, the Derived class too will be an Abstract Class.
Now Question Arises:
What is the Use of Generating another Abstract Class?
Abstract Class are mainly for Generalisation, having some common functionalities implemented.
A Derived Abstract Class (i.e. if a Class, derived from ABS class, does not implement all of PVFs), will be considered as SUB-GENERALISATION.
Ex:
Living Being is a generalise class, with some common functionalities of Animals, Birds, Sea creatures. The common functionalities may be eating, breathing.
Animal is a Sub Generalise Class, with some common functionalities of Cat Family Animals and Dog Family Animals.

I hope, I was clear.

Pl check the same on C++ programming too.

  Was this answer useful?  Yes

Abhijeet Gawali

  • Apr 20th, 2018
 

Yes, if u Create Abstract class one or more abstract method in class.
If after inherit Abstract Class all abstract methods are compulsory/Mandatory implemented in derived class., otherwise they generate error. Please see attached example for reference.
For e.g. does not implement inherited abstract member Rextester.Cars.getTotalSeat()

Code
  1. //Rextester.Program.Main is the entry point for your code. Dont change it.

  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

  3.  

  4. using System;

  5. using System.Collections.Generic;

  6. using System.Linq;

  7. using System.Text.RegularExpressions;

  8.  

  9. namespace Rextester

  10. {

  11.     public class Program

  12.     {

  13.         public static void Main(string[] args)

  14.         {

  15.             //Your code goes here

  16.             Console.WriteLine("Hello, world!");

  17.             Toyota Toy = new Toyota();  

  18.            

  19.             Console.WriteLine(Toy.Wheel());  

  20.             Console.WriteLine("Discount Price :{0}", Toy.DiscountPrice());  

  21.              Console.WriteLine("Total ONRoad Price:"+ Toy.price());  

  22.             Console.WriteLine(Toy.getTotalSeat());  

  23.             Console.WriteLine(Toy.colors());  

  24.         }        

  25.     }

  26.    

  27.     public abstract class Cars

  28.     {

  29.         public abstract double price();  

  30.         public abstract int getTotalSeat();  

  31.         public abstract string colors();  

  32.          public string Wheel()  

  33.         {  

  34.             return "4 wheeler";  

  35.  

  36.         }  

  37.     }

  38.      public class Toyota :Cars

  39.      {

  40.           public string DiscountPrice()  

  41.         {  

  42.             return "20% discount on buying Toyoya Cars";  

  43.         }  

  44.         public override double price()  

  45.         {  

  46.             return 1000000.00;  

  47.         }  

  48.         /*public override int getTotalSeat()  

  49.         {  

  50.             return 5;  

  51.         } */

  52.         public override string colors()  

  53.         {  

  54.             return "Red";  

  55.         }  

  56.      }

  57. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions