Jump to content
Welcome, welcome! Come in and register, and have some developer coffee. 👨‍💻 ×
  • 1

OOP Principles?


Go to solution Solved by S.,

Question

Quote

Your program should meet all the requirement as below:

  1. Apply the 3 object-oriented programming principles; encapsulation, inheritance and basic polymorphism in your solution;

  2. The class should have at least one (1) new attribute, one (1) new method and one (1) constructor;

  3. The class should contain at least one (1) setter method or getter method to set or to retrieve the value(s) of class’ attribute(s);

I am poor in OOP principles. Can someone give an example?

 

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 1
  • Solution

The 3 requirements are kind of what "forms" a class. Below explanations are written in C# and the concept should be applicable to other OOP languages as well.

Encapsulation, as the name suggests, simply means we "encapsulate"(hide) all variables in a class and it is not assessible outside the class instance. You can only access the variable through a function of the class. Note that it is also not accessible with an instance of the class*.

public classs Student {
	//hidden member variables / attributes*
	private int age; 
  
  	//Call this function with a class instance to retrieve age
  	public int getAge(){
		return age;
	}
}

//In your main()
public static void Main(string[] args) {
  Student studentA = new Student(); // Declare an instance of a class
  Console.WriteLine(studentA.age); // You will get an error here: 'Student.age' is inaccessible due to its protection level
  
  Console.WriteLine(studentA.getAge()) // This is valid. Note that we never assigned a value for age so it'll default to 0
}

 

A constructor is a method that will automatically gets invoke when you create a class instance. Below are some of the things you need to note when creating a class constructor:

Quote
  • Constructor of a class must have the same name as the class name in which it resides.
  • A constructor can not be abstract, final, and Synchronized.
  • Within a class, you can create only one static constructor.
  • A constructor doesn’t have any return type, not even void.
  • A static constructor cannot be a parameterized constructor.
  • A class can have any number of constructors.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor. 

Reference: https://www.geeksforgeeks.org/c-sharp-constructors/

 

Here's how you declare a constructor. It can be used to instantiate the member variables of a class.

public class Student {
	private int age;
  
	public Student(int age) {
		this.age = age; // the 'this' keyword refers to the class instance
	}
}

 

A method in another word is a function of a class. We already have an example in the first code; getAge() is our method.

Getters and Setters are methods used to retrieve and assign values to a class attribute. In our previous example, getAge is actually our getter. To define a setter, add the following method to your class:

public void setAge(int ageInput)
{
    age = ageInput;
}

 

There's also a shorthand code for writing the properties / getters and setters. Check out https://codeasy.net/lesson/properties

Quote

A Property, or a getter or setter, can implement additional logic, for example:

Calculating a value based on some private data (rather than just returning a value directly)

Checking and validating data provided (to prevent overflows or out of range input).

Reference: https://stackoverflow.com/a/6745448

 

Hopefully that helps. On a side note tho, I haven't been writing codes in OOP so feel free to correct me if any of my explanation is incorrect. 😃

  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...