oops concept query

0 votes

This is my oops question.

This is my base class

public class BaseClass
{
   public int i = 10;
   public int x = 30;
   public string str = "Hello";
   public virtual string Hello()
   {
     return "Hello of base class called";
   }
}

This is my child class

public class ChildClass : BaseClass
{
    public int i = 20;
    public int z = 90;
    public override string Hello()
    {
       return "Hello of child class called";
     }
}

I have noticed that following works fine

BaseClass baseObject = new ChildClass();

and if I enter baseObject, then only members of BaseClass are shown

  1. Can someone provide me some advice on a circumstance in which a developer needs to do something like BaseClass baseObject = new ChildClass();?
  2. Why are my child member variables not accessible through this baseObject if my BaseClass object has a reference to my child class object?
Jun 11, 2022 in C# by jyoti
• 1,240 points
412 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
To answer your first question, you might need something like baseclass when there is a need to have objects with individual behavior that possess a common function set. For instance, you might want to put objects of different data types into a list.

Now, for the second question about why child member variables are not accessible, the underlying type is going to be ChildClass but you will still be working on a BaseClass type. This is the reason you will only see the members of BaseClass. It is possible for the conversion of BaseClass instance to ChildClass with something like a cast operation or the "as" keyword.
answered Jul 4, 2022 by krishna
• 2,820 points

edited Mar 5

Related Questions In C#

0 votes
0 answers

How to do a LIKE query with linq

How can I use Linq to conduct ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
565 views
0 votes
0 answers

Object Oriented Programming Basic Concept(C#)

We are aware that the default modifier ...READ MORE

Dec 12, 2022 in C# by Roshan
• 300 points
597 views
0 votes
0 answers

oops concept query

This is my oops query. This is my ...READ MORE

Sep 27, 2022 in C# by krishna
• 2,820 points
377 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
962 views
0 votes
1 answer

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
779 views
0 votes
0 answers

How to force overriding a method in a descendant, without having an abstract base class

This is my code using System; using System.Collections.Generic; using System.Linq; using ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
1,106 views
0 votes
2 answers

Explain OOPs concept in python with programatic examples

OOP reflects the real world behavior of ...READ MORE

answered Jun 24, 2020 in Python by rahul
• 360 points
2,154 views
+1 vote
1 answer

R query and Data Science

Dear Deepika, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,026 views
0 votes
1 answer

How Impala is fast compared to Hive in terms of query response?

Impala provides faster response as it uses MPP(massively ...READ MORE

answered Mar 21, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
2,451 views
+16 votes
4 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP