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
Post a Comment