Inheritance + NestedClasses in C #

We can have nested classes in C #. These nested classes can also inherit OuterClass. For example:

public class OuterClass { // code here public class NestedClass : OuterClass { // code here } } 

quite acceptable.

We can also achieve this by not making NestedClass a nested class in OuterClass, as shown below:

 public class OuterClass { // code here } public class NestedClass : OuterClass { // code here } 

I am wondering what is the difference between the two above scenarios? What can be done in scenario I, which cannot be implemented in scenario II? Is there something we get more by making NestedClass "nested" in OuterClasss?

+6
source share
4 answers

the second example you provided is not a nested class, but a regular class derived from OuterClass .

  • Nested types are displayed as private by default, but can be declared with wider visibility
  • Nested types can refer to the properties, fields, and methods of the containing type (even declared private and those inherited from base types)

also consider here when and why to use nested classes.
MSDN Link: Nested Types (C # Programming Guide)

EDIT
To refer to @Henk, comment on the difference in the nature of both relationships (inheritance versus nested types): In both cases, you have a relationship between the two classes, but they are of a different nature. When derived from a base class, a derived class inherits all (except private ) methods, properties, and fields of the base class. This is not true for a nested class. Nested classes do not inherit anything, but have access to everything in the containing class — even private fields, properties, and methods.

+13
source

Inheriting a parent class prevents a nested class from seeing its parent private members and methods, only protected (and public). Nesting it in the parent class allows you to see all private members and call its private methods, regardless of whether the nested class inherits the parent class or not.

+4
source

Maybe it's too late But let me add my 2 cents. Please, if I understand your question correctly, you mean:

What is the advantage of a nested class that also inherits from its outer class?

The key is design

First code:

  public class OuterClass { public OuterClass{console.writeln("OuterClaass Called");} public class NestedClass : OuterClass //It does Inherit { public NestedClass{console.writeln("NestedClass Called");} } } static void Main() { outerClass.NestedClass nestedobject = new outerClass.NestedClass(); } 

OutPut:

The outer class is called

NestedClass Called

Second code:

 public class OuterClass { public OuterClass{console.writeln("OuterClaass Called");} public class NestedClass //it dosent Inherit { public NestedClass{console.writeln("NestedClass Called");} } } static void Main() { OuterClass.NestedClass nestedobject = new OuterClass.NestedClass(); } 

Exit:

NestedClass is called


In the first code, when constructing a NestedClass object, the OutrClass constructor will also be called and, in my opinion, this means the ratio of components between the NestedClass and OuterClass But. In the second code, the object construction for NestedClass and Outerclass is not limited together and is executed independently.

Hope this will be helpful.

+3
source

Nested classes differ from subclasses in how they can access the properties and private fields of a container class when inheriting from it.

Nested classes are a combination of inheritance and encapsulation in OOP in terms of implementing a singleton design template, where dependencies are well hidden, and one class provides a single access point with static access to internal classes, while instant capability is supported.

For example, using a practical class to connect to a database and insert data:

 public class WebDemoContext { private SqlConnection Conn; private string connStr = ConfigurationManager.ConnectionStrings["WebAppDemoConnString"].ConnectionString; protected void connect() { Conn = new SqlConnection(connStr); } public class WebAppDemo_ORM : WebDemoContext { public int UserID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Phone { get; set; } public string Email { get; set; } public string UserName { get; set; } public string UserPassword { get; set; } public int result = 0; public void RegisterUser() { connect(); SqlCommand cmd = new SqlCommand("dbo.RegisterUser", Conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("FirstName", FirstName); cmd.Parameters.AddWithValue("LastName", LastName); cmd.Parameters.AddWithValue("Phone", Phone); cmd.Parameters.AddWithValue("Email", Email); cmd.Parameters.AddWithValue("UserName", UserName); cmd.Parameters.AddWithValue("UserPassword", UserPassword); try { Conn.Open(); result = cmd.ExecuteNonQuery(); } catch (SqlException se) { DBErrorLog.DbServLog(se, se.ToString()); } finally { Conn.Close(); } } } } 

The WebAppDemo_ORM class is a nested class inside a WebDemoContext and at the same time inherits from WebDemoContext so that the nested class can access all members of the container class, including private members that can be effective in reducing DRY and achieving SOC .

0
source

Source: https://habr.com/ru/post/898006/


All Articles