c# - Why should I inherit? -


sorry if foolish question, m confuced

namespace test {     public class class1     {         public string property1 { get; set; }         public void method1()         {          }     }      public class class2 : class1     {        public void method2()        {            property1 = "set  class2";            method1();        }      }      public class class3      {         class1 objclass1 = new class1();         public void method3()         {             objclass1.property1 = "set  class3";             objclass1.method1();         }      } } 

if can access public methods of class1 class3 using object of class1, s advantage of inheritance (as done in class2) ?

in oops, concept of inheritance provides idea of reusability. means can add additional features existing class without modifying it. possible deriving new class existing one. new class have combined features of both classes.

  • once behavior (method) or property defined in super class(base class),that behavior or property automatically inherited subclasses (derived class).

  • code reusability increased through inheritance.

  • inheritance provide clear model structure easy understand without complexity using inheritance, classes become grouped in hierarchical tree structure code easy manage , divided parent , child classes.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -