+ 10
Classes in Object Oriented Programmimg
I'm an OOP noob. Pretty much every beginner tutorial I see on OOP uses animal -> mammal -> cat type example for classes and inheritance. This is helpful to get the simple idea ... but, I'd like more concrete and realistic examples to get an idea of what I'd really use it for. Can you help provide me with real life examples of class and inheritence that I might expect to come across in a professional programming situation? I'd love to see examples of any kind that are used in the real world. Thanks!
9 Respuestas
+ 7
thank you @Segev Elmalech
+ 6
@sneeze, @Bagshot, @Insight
thanks all of you for taking the time. these were helpful!!
+ 5
Scientific Example:
We would like to work with big integer numbers and define special functionalities for them.so we should create a class and add properties and methods to it.
Game Example:
Imagine we are thinking about a game with a ball doing fancy stuff. again we create a ball class and implement for example some formulas for its movements.
Also about inheritance, it's better to define a basic ball class and then for different types of balls, we will change or add some details.
If these are not sufficient, tell me to explain more.
+ 5
how it can be called derived class
+ 2
One way to look at classes is if they are blueprints to create objects. So if I was writing a blog engine, for example, then I could write and read information from a database using a class like this:
public class BlogPost
{
public string Title { get; set; }
public string Content { get; set; }
}
This class has the string properties "Title" and "Content". As of yet, these properties have no values. So to use this class I would create an object (blogPost) that has access to these properties:
var blogPost = new BlogPost()
{
Title = "My first post.",
Content = "Some content for testing."
};
Now my object has some values -- in this case manually entered strings, but in real life, probably retrieved a database -- they can be displayed in a UI, for this example it's the console:
Console.WriteLine(blogPost.Title.ToUpper() + "\r\n" + blogPost.Content);
OOP makes handling data clean and efficient.
https://code.sololearn.com/cQ7U4rWZ6SPn
+ 2
A college example : Start with students, teachers. Both deriving from a people class.
In the people class age and address is defined. In the student class study and grades are defined. In the teacher class wage, payment, competents are defined.
+ 1
I have an article page on my app.
The article could be with different types like article with text only or image only. So I have created an Article class as super and class for each type as subclass because there are common properties like type and different properties like images or texts.
+ 1
Rt @Viktor
0
i like real world application examples:
abstract class Action{
protected $_response =“”;
abstract public function processAction();
public function getResponse(){
return $this->_response;
}
}
class PlaceOrder extends Action{
public function processAction(){
$this->_response = “order placed”;
}
class CancelOrder extends Action{
public function processAction(){
$this->_response = “order canceled”;
}
now you can ecapsulate all your logic for each action. you can easily run any action because they all share the same methods, and you can easily add actions.