-1

I have a class Person (Parent class) which contains some properties. Let's say 2 properties. I want to access 1 properties out of 2 properties in Student (child class) from Person class(Parent class).

Note: All properties are public which I need to use in other child class.

How will I achieve that using C#? (This applies to any object oriented programming languages)

Below is my sample code.

using System;  

public class Person  
{  
   public string name; //only want this property in all child classes
   public float salary;  //don't want to access this property in Student
}

public class Student: Person  
{  
   public string subject;  
}

public class Employee: Person  
{
   public int employeeId;
}
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    `This applies to any object oriented programming languages` No, it does not. Please tag your question *only* with the language you're using. – CertainPerformance Jan 31 '19 at 10:09
  • What have you tried? Because this is extremely basic OOP/C# knowledge - assuming you've gotten to this material, you should have covered accessing them. – VLAZ Jan 31 '19 at 10:09
  • what exactly are you stuck on? `Student` and `Employee` both have access to `name`, it's as simple as `this.name`? *don't want to access this property* but you've made it `public`, so it's, you know, public. anything can access it? If you want to restrict access to it make it `private` or `protected` depending on what you actually want – Liam Jan 31 '19 at 10:13
  • Just to clarify: Property:`public string Name { get; set;}` Field:`public string name;` – Chrᴉz remembers Monica Jan 31 '19 at 11:27

3 Answers3

0

You shouldn't have salary as a field in Person unless all Persons have a salary, this should instead go in the Employee class, or the highest class which uses salary

AleksW
  • 703
  • 3
  • 12
0

There is a conceptual problem in your code! The salary property is not general enough to be in the person class (not every person has a salary). You should not include this property in the Person class. Using an interface would help only if:

  • you have multiple subclasses and some of them have salaries
  • you need to manage the subclasses that have salaries as a group without knowing the particular type of each one (e.g. polymorphism).

Hope that helps!

user1503486
  • 3
  • 1
  • 3
-1

You can use an interface to achieve what you are aiming for. It doesn't stop the compiler from creating a Salary property for student object. But by using IStudent, you can restrict the access of the end user.

public class Person
{
    public string Name { get; set; } //only want this property in all child classes
    public float Salary { get; set; } //don't want to access this property in Student
}
interface IStudent
{
    string Name { get; set; }
    string Subject { get; set; }
}

public class Employee : Person
{
    public int EmployeeId { get; set; }
}

public class Student : Person, IStudent
{
    public string Subject { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IStudent s = new Student() { Name = "Student1", Subject = "Subject1" };
        Console.WriteLine(s.Name);
    }
}
Carbine
  • 7,849
  • 4
  • 30
  • 54
  • Nothing is "restricted" here. The property is just masked by the interface. `((Student)s).Salary` or for that matter `((Person)s).Salary` – Liam Jan 31 '19 at 11:42