Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can i create object of child class using parent class like this:
manager obj=new employee if yes then where is it needed? and how at memory level it is implemented
thank u for response

What I have tried:

i am new to C# and oops . this is my first encounter with c# and it has been just 15 days with c# so help me to learn
Posted
Updated 24-Jul-16 2:01am

No you cannot, since the Manager class is more than just an Employee. You can do it the other way round, but that really makes no sense.
 
Share this answer
 
It depends.
If your classes are declared correctly, then yes - but since a manager is an employee, but an employee does not have to be a manager, probably not.
Normally, you would have this:
C#
class Employee
   {
   string Name {get; set;}
   int EmployeeNumber {get; set;}
   Employee ReportsTo {get; set;}
   }
class Manager : Employee
   {
   Car CompanyCar {get; set;}
   }

And then you can happily say:
C#
Employee emp = new Manager();
Becuase a Manager is a "superset" of an employee.
You can then access any Employee properties:
C#
Employee emp = new Manager();
...
Console.WriteLine(emp.Name);
But...because emp can contain "any employee" you can't access any Manager specific properties of methods via it.
C#
Manager man new Manager();
Employee emp = man;
...
Console.WriteLine(emp.Name);
Console.WriteLine(man.CompanyCar);

But you can't do this:
C#
Manager man = new Employee(); 
Because an Employee does not have any of the Manager specific properties of methods: in this case because an Employee does not get a CompanyCar, while Managers do.
 
Share this answer
 
Comments
Member 12650701 25-Jul-16 4:17am    
GREAT EXPLANATION SIR.THANKS
OriginalGriff 25-Jul-16 4:31am    
You're welcome!
Welcome to the wonderful adventure of learning C# !

Remember that everyone here was once a beginner, and we all ... I guarantee you ... learned what we do know, now, by making many mistakes. Most of us have learned that technical learning is an on-going process involving making multiple "passes" over the same material over time.

You need to start with some diligent study and practice of the basics. Get a good introductory on C# and .NET; here's a good one you can download for free, right now: Charles Petzold, ".NET Book Zero: [^]. There is a very good free book in English created by a group of Bulgarian C# programmers you can download here: [^].

I strongly recommend you get the Bulgarian book and carefully review:

1. first Chapter 14,"Defining Classes," pages #499 ff.

2. Chapter 11., "Creating and Using Objects," pages #385 ff.

Hands-on practice is so important; you need to get Visual Studio set-up if you haven't already, and take your book, and code in the examples; run them, observe what happens. When errors occur ... and they will occur ... put break-points in your code, and then run the code.

Learning to debug is critical, and when your code stops at a break-point, then you can use F11 in Visual Studio to single-step through the code, observing the values of variables and objects by hovering over their names in the code.

imho these are some things that will help you learn:

1. try to find the right pace and "mix" of study and experimentation: the "right pace" will leave you curious and eager to learn ... trying to learn too much too fast will result in your being frustrated.

2. use the documentation provided by Visual Studio that's so easily accessible.

3. spend time here on CodeProject reading some of the many excellent tutorials on every aspect of C# and .NET.

4. Classes and inheritance: remember that a Class, or Struct, definition is a template for the construction of instances (objects) when the compiled code you write is executed (at run-time).

There are many powerful facilities that are available for implementing OOP and inheritance in C#, including Interfaces, Abstract classes, Virtual methods, the ability to over-ride methods, etc.

Understand that for most people it's going to take some months to really reach a deeper mastery of those tools.

5. C#: as with any computer language you are going to need to understand:

a. creation of variables, and properties. understanding how variables can be passed (and returned) to methods (as parameters) by reference and by using the 'out modifier. the access modifiers that put in front of variable/property declarations (private, public, protected, etc.).

b. creation of data structures like Arrays. creating generic data structures.

c. writing methods with and without parameters, writing methods that return some value(s), and methods that return no value (void).

d. flow-of-control: for-loop, while-loop, switch statement. try/catch/finally. use of 'break and 'continue.

As you learn and get "stuck" (as we all do), try to carefully compose the questions you ask here, or on other forums. Try and describe specific issues, and clearly describe what error occur (if any). Show carefully selected parts of your code relevant to the problem.

And, now, let's get started studying ... :)
 
Share this answer
 
v3
Comments
Member 12650701 25-Jul-16 4:15am    
Thanks sir BillWoodruff for encouraging and giving me clear guidelines to be conversant c#.THANKS A LOT.
BillWoodruff 25-Jul-16 4:40am    
You are welcome !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900