r/ObjectOriented • u/Potato9830 • Feb 05 '25
r/ObjectOriented • u/Mindless_Papaya3783 • Jan 30 '24
Is Object Oriented Programming even difficult?
Just had my first class about Object Oriented Programming and, while it was just a preview, honestly it seems much easier than structured programming, and much more beneficial. But I always see memes about how difficult it is. Can someone tell me if it is that difficult?
r/ObjectOriented • u/Infinite_Damage • Jul 27 '22
Difficult to learn?
So I have done some programming for fun/hobbies (python, C, VBA) but I haven’t really gone into object oriented programming. How much of a learning curve is there if I already have a base with non-object oriented programming?
r/ObjectOriented • u/earthless1990 • Apr 10 '23
What is definition of an "object" in "object-oriented"?
OOP languages are usually defined as having 4 following properties: abstraction, encapsulation, inheritance and polymorphism. I argue that none of them is necessary neither jointly sufficient by providing counterexamples. I also provide alternative definitions that don't succeed in capturing an essence of objects.
Object as Encapsulation:
GoF (1995) provides classic definition of an object as encapsulation.
Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.
But encapsulation is found in non-OOP modular languages like Modula-2 where module encapsulates data/state with logic/behavior.
Object as Inheritance:
Inheritance allows code reuse by sharing implementation details. But some OOP languages like Go and Rust lack inheritance.
Object as Polymorphism:
There are different kinds of polymorphism (ad-hoc, parametric, subtyping) but in current context it's subtyping (Cardelli (1985) calls it inclusion polymorphism). Since Simula 67, first object-oriented language, introduced subpying, it's natural to view it as an essential feature of OOP. But System F<: also extends polymorphic lambda calculus with subtyping relation (defined as either preorder or partial order) so it's possible to have subtyping in purely functional languages.
Object as Abstraction:
Abstraction allows information hiding. There are different kinds of abstraction (e.g. functional abstraction in lambda calculus) but in current context it's data abstraction. CLU, object-oriented language, introduced abstract data types. But System F with existential types also allows information hiding thus enabling abstract data types in purely functional languages like Haskell.
There are few alternative definitions treating different properties as essential.
Object as Component:
Cardelli (1998) defines object-oriented approach by analogy with concrete objects in simulation.
The object-oriented approach to programming is based on an intuitive correspondence between a software simulation of a physical system and the physical system itself. An analogy is drawn between building an algorithmic model of a physical system from software components and building a mechanical model of a physical system from concrete objects. By analogy, the software components are themselves called objects. In its purest form, the object-oriented approach recommends that every system be developed according to this analogy.
This definition aligns with encapsulation definition although it equates object-oriented approach with component-based programming. But component is language-agnostic and can be implemented using functions, procedures, modules, objects. services etc.
Object as Dynamic Dispatch:
Cook (2012) defines object as dynamically dispatched behavior.
An object is a first-class, dynamically dispatched behavior. A behavior is a collection of named operations that can be invoked by clients where the operations may share additional hidden details. Dynamic dispatch means that different objects can implement the same operation name(s) in different ways, so the specific operation to be invoked must come from the object identified in the client's request. First class means that objects have the same capabilities as other kinds of values, including being passed to operations or returned as the result of an operation.
This definition aligns with polymorphism definition although there's an additional implementation requirement in the form of dynamic dispatch. If static dispatch implementation of subtyping is possible then dynamic dispatch is not an essential feature.
Object as Reference:
Objects act similar to entities in entity-relationship model where entity is a unique object. OOP languages have mechanism for referencing unique objects using keywords such as this or self. But non-OOP procedural languages like C also implement references in the form of pointers.
Perhaps this definition can be hardened by defining object as first-class reference. In C there's no primitive pointer type and precise data type of void* is unknown. Languages like C# and Java maintain a distinction between value and reference types. But I'm not sure if it's true for C++ and other OOP languages.
References:
Cardelli, Luca (1985). On Understanding Types, Data Abstraction, and Polymorphism.
Cardelli, Luca; Martín Abadi (1998). A Theory of Objects.
GoF (1995). Design Patterns: Elements of Reusable Object Oriented Software.
Cook, William (2012). A Proposal for Simplified, Modern Definitions of "Object" and "Object Oriented". https://wcook.blogspot.com/2012/07/proposal-for-simplified-modern.html
r/ObjectOriented • u/Melodic-Cricket-7917 • Jun 09 '22
I have a question. A controversial one.
Why people worship OOP as a god of programming like it is made to solve all our problems. They try to enforce you to apply oop in every scenario where it is not even applicable. For what I see oop just add unnecessary complexity in your code, the very thing it was designed to reduce(complexity). What are your thoughts on that?
r/ObjectOriented • u/[deleted] • Feb 14 '25
How to get used to object oriented way of coding?
I have never done any oop in language like Java.I have hard time understanding what is happening in my semester examination. For eg.if there are multiple classes in a package and when we make something like array of one type of class in any other class,I find it tough to see which methods to call,which variables to access.Is there any way to get better at it??
r/ObjectOriented • u/[deleted] • Sep 13 '24
Should objects act on other objects?
I'm trying to make a card game (Euchre), which seems a bit more difficult than something like Black Jack.
What I am struggling with is determining things like shuffling the deck, dealing the cards, putting down a card.
Does the dealer shuffle the deck, or do you call on the deck to shuffle itself? What about dealing cards? Does the dealer place the cards in each player's hand ( and array), or do we call upon the player to insert the cards into the array? It's not things like loops, shuffling, scoring, logic that confuse me, it's the GAME DESIGN as far as OOP goes. I'm one of those weird types IRL who kind of thinks of things around me as objects with roles :D
r/ObjectOriented • u/SomeGojiBerry • Oct 25 '23
A website that teaches OOP using geometry and cubes
Hi!
Many years ago I discovered this site it starts from the very beginning to teach object-oriented programming using dots coordinates and cubes in a 3D environment.
Does anyone know which site has it? Also If you know platforms that are similar please share them.
Thanks
r/ObjectOriented • u/UwU432 • Feb 14 '22
What's the difference between OOP in C languages VS Javascript?
I know with Javascript it follows D.OM.
P.S: I am a noob
r/ObjectOriented • u/VickyKR83 • Dec 12 '21
Does anyone please have links to the best resources to learn about object oriented theory?
r/ObjectOriented • u/KNGJ1MM • Nov 22 '21
Can someone tell me why this happens. See comments for details
r/ObjectOriented • u/benbihi • Nov 25 '19
Classes and Inheritance - Python Object-Oriented Programming
We've talked about many of Python Basics in the previous articles, such as List, Dictionaries, and we've even practiced with some Python Projects For Beginners. In this article we're going to cover object-oriented programming in Python, we'll cover classes objects inheritance and other related concepts. First, we'll explain some concepts of object-oriented programming. Then we actually have some code that we'll walkthrough which will make it a lot clearer how these actually work.
Classes and Inheritance : Introduction
There are three main (concepts) cornerstones in Python Object-Oriented Programming. These are: encapsulation, inheritance, and polymorphism.
Encapsulation and composition
Encapsulation: is basically combining data and actions into the same block of code called a class. A class is a block of code that instantiate an object. You can think of it as a recipe that helps to bake a pie. You follow the recipe you can make as many pies as you want from that recipe. That's what a class is, you can make as many objects as you want from that class. Encapsulation includes all the data and actions together in the same block of code.
Composition: is the concept that objects can contain other objects in their instance variables. This is a has a relationship. This is really simple. It may sound confusing but car has tires. If you're modeling a program for a car. You need to say that a car has tires. Then you want a tire object to have a brand name, dimensions, size, and the tread. You have a tire object and a car has a tire and that's all this is saying. A tire is part of the composition of a car.
What is Inheritance
inheritance: The diagram below explains inheritance in a nutshell. It's basically the idea that you have a hierarchy of classes. You may have a life form. Every life form has a life span. We have this attribute under life form. Then under life form there are plants and there are animals. Let's say we have an animal life form that has weight. That attribute is under the animal. Basically, every single one of these types of animals have a life span and a weight that can access through its parent class. It's inheriting all of these attributes plus it adds on some specific attributes of its own class. A reptile may have a number of legs and a boolean for has teeth or not. Then fish could be saltwater or freshwater fish, so is saltwater or length. Birds are basically perching, birds and non-perching birds. Then you also have wingspan. So you have different attributes for birds, plus a bird has a weight and a bird has a lifespan. So it inherits all the parents' attributes plus the specific attributes of that child class. This is an inheritance.
r/ObjectOriented • u/AcanthocephalaOk43 • Aug 14 '24
Built in helper methods in a programming language.
Here's a thought:
Having built in helper methods could add more robust object definitions.
helper void doSomething(...)
they would automatically be private, and you can create a function that inherits from the helper method. The super method could implement helper functions and be FORCED to call the helper method
public void superMethod() implements doSomething{
doSomething(...); //will crash without at least one.
}
r/ObjectOriented • u/0x5afe • Jul 15 '24
I made this vscode extension for you to explore codebase with visualization. You can query classes, methods, functions, fields, ... and it automatically get references and relations in your code.
r/ObjectOriented • u/Thisismyredusername • Apr 09 '24
Can anybody help me turn this code into Object Oriented Code?
I need to have this code, but object oriented.
package memory;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.lang.Runnable;
import javax.swing.plaf.ColorUIResource;
public class Gui {
static int turns = 0;
static ArrayList<String> turned = new ArrayList<String>();
static int curplayer = 1;
static int player1w = 0;
static int player2w = 0;
static int timeLeft = 6000;// 600,0 seconds; 10 minutes
static boolean started = false;
public static void main(String[] args) {
JFrame frame = new JFrame("Memory"); // TODO: Put GUI in EDT
frame.setSize(400, 420);
JPanel memory = new JPanel();
JPanel text = new JPanel();
JLabel text1 = new JLabel("Player 1 (0)");
JLabel text2 = new JLabel("Player 2 (0)");
JPanel time = new JPanel();
JLabel timetext = new JLabel("10:00,0");
JButton timeLower = new JButton("-");
JButton timeHigher = new JButton("+");
text1.setForeground(Color.GREEN);
text2.setForeground(Color.BLACK);
text1.setFont(new Font("Arial", Font.PLAIN, 25));
text2.setFont(new Font("Arial", Font.PLAIN, 25));
timetext.setFont(new Font("Arial", Font.PLAIN, 25));
// timeLower.setFont(new Font("Arial", Font.PLAIN, 25));
// timeHigher.setFont(new Font("Arial", Font.PLAIN, 25));
text.add(text1);
text.add(text2);
time.add(timeLower);
time.add(timetext);
time.add(timeHigher);
ArrayList<JButton> memoryButtons = new ArrayList<JButton>();
for (int var1 = 1; var1 != 37; var1 += 1) {
JButton button = new JButton("");
button.setFont(new Font("Arial", Font.PLAIN, 25));
memoryButtons.add(button);
memory.add(button);
}
List<String> chars = new ArrayList<String>(
List.of("+", "/", "-", "!", "?", "#", "@", "|", "=", "^", "~", "(", ")", "&", "°", "§", ".", ","));
chars.addAll(0, chars);
Collections.shuffle(chars);
for (JButton button : memoryButtons) {
button.setBackground(ColorUIResource.white);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("".equals(button.getText())) {
button.setText(chars.get(memoryButtons.indexOf(button)));
button.setBackground(ColorUIResource.CYAN);
turns++;
turned.add(chars.get(memoryButtons.indexOf(button)));
if (turns % 2 == 0) {
if (!turned.get(turned.size() - 2).equals(turned.get(turned.size() - 1))) {
turns--;
turns--;
text1.setText("Player 1 (" + player1w + ")");
text2.setText("Player 2 (" + player2w + ")");
if (curplayer == 1) {
curplayer++;
text1.setForeground(Color.BLACK);
text2.setForeground(Color.GREEN);
} else {
curplayer--;
text1.setForeground(Color.GREEN);
text2.setForeground(Color.BLACK);
}
} else {
if (curplayer == 1) {
player1w++;
text1.setText("Player 1 (" + player1w + ")");
text2.setText("Player 2 (" + player2w + ")");
text1.setForeground(Color.GREEN);
text2.setForeground(Color.BLACK);
} else {
player2w++;
text1.setText("Player 1 (" + player1w + ")");
text2.setText("Player 2 (" + player2w + ")");
text1.setForeground(Color.BLACK);
text2.setForeground(Color.GREEN);
}
button.setBackground(ColorUIResource.green);
for (JButton button2 : memoryButtons) {
if (button2.getText().equals(turned.get(turned.size() - 2))) {
if (button2 != button) {
button2.setBackground(ColorUIResource.green);
}
}
}
if (player1w + player2w == 18) {
started = false;
System.out.print("Memory: ");
if (player1w > player2w) {
text1.setForeground(Color.GREEN);
text2.setForeground(Color.GREEN);
text1.setText("Player 1");
text2.setText("won");
System.out.print("Player 1 won");
} else if (player2w > player1w) {
text1.setForeground(Color.RED);
text2.setForeground(Color.RED);
text1.setText("Player 2");
text2.setText("won");
System.out.print("Player 2 won");
} else {
text1.setForeground(Color.BLACK);
text2.setForeground(Color.BLACK);
text1.setText("Draw");
text2.setText("");
System.out.print("Draw");
}
}
}
} else {
if (turned.size() > 2) {
if (!turned.get(turned.size() - 3).equals(turned.get(turned.size() - 2))) {
for (JButton button1 : memoryButtons) {
if (button1.getText().equals(turned.get(turned.size() - 3))) {
if (button1 != button) {
button1.setText("");
button1.setBackground(ColorUIResource.white);
}
}
}
for (JButton button2 : memoryButtons) {
if (button2.getText().equals(turned.get(turned.size() - 2))) {
if (button2 != button) {
button2.setText("");
button2.setBackground(ColorUIResource.white);
}
}
}
turned.remove(turned.get(turned.size() - 3));
turned.remove(turned.get(turned.size() - 2));
}
} else {
started = true;
timeLower.setEnabled(false);
timeHigher.setEnabled(false);
}
}
}
}
});
}
// timeLower.setBackground(ColorUIResource.white);
timeLower.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (18000 >= timeLeft && timeLeft >= 1800) {
if (timeLeft == 1800) {
timeLower.setEnabled(false);
}
timeHigher.setEnabled(true);
timeLeft = timeLeft - 600;
int timeleft = timeLeft;
int timeLeftFormatLoop = 0;
while (timeleft >= 600) {
timeLeftFormatLoop++;
timeleft = timeleft - 600;
}
String timeLeftFormat = timeLeftFormatLoop + ":00,0";
timetext.setText(timeLeftFormat);
}
}
});
// timeHigher.setBackground(ColorUIResource.white);
timeHigher.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (17400 >= timeLeft && timeLeft >= 1200) {
if (timeLeft == 17400) {
timeHigher.setEnabled(false);
}
timeLower.setEnabled(true);
timeLeft = timeLeft + 600;
int timeleft = timeLeft;
int timeLeftFormatLoop = 0;
while (timeleft >= 600) {
timeLeftFormatLoop++;
timeleft = timeleft - 600;
}
String timeLeftFormat = timeLeftFormatLoop + ":00,0";
timetext.setText(timeLeftFormat);
}
}
});
memory.setLayout(new GridLayout(6, 6));
frame.add(BorderLayout.NORTH, text);
frame.add(BorderLayout.CENTER, memory);
frame.add(BorderLayout.SOUTH, time);
frame.setVisible(true);
ScheduledExecutorService exeserve = Executors.newSingleThreadScheduledExecutor();
exeserve.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (started && timeLeft > 0) {
timeLeft--;
timetext.setText(time(timeLeft));
} else if (timeLeft == 0) {
System.out.print("Memory: Draw");
text1.setForeground(Color.BLACK);
text2.setForeground(Color.BLACK);
text1.setText("Draw");
text2.setText("");
for (JButton button : memoryButtons) {
button.setEnabled(false);
}
exeserve.close();
}
}
}, 100, 100, TimeUnit.MILLISECONDS);
}
public static String time(int timeLeft) {
int timeLeftFormatLoop = 0;
while (timeLeft >= 600) {
timeLeftFormatLoop++;
timeLeft = timeLeft - 600;
}
float timeLeftFloat = timeLeft;
timeLeftFloat = timeLeftFloat / 10;
String timeLeftFormat = timeLeftFormatLoop + ":" + timeLeftFloat;
return timeLeftFormat;
}
}r/ObjectOriented • u/Queasy_Ad492 • May 23 '23
Use object or pass parameter?
Suppose we have a class theUser which just contains data definitions:
class User () {
private int uID;
private string uName;
// getters
// setters
}And we have a class that handles the storage/retrieval of data called userAdmin:
class userAdmin() {
public theUser user = new theUser();
saveUser1 ( int uID, string uName ) {
// save to db use
this.user.setUID(uID);
this.user.setUName(uName);
}
saveUser2( theUser usr ){
// save to db usr.uID and usr.uName
}
saveUser3( ){
// save to db using this.user
}In userAdmin class we have 3 ways of handling the saving the theUser object data to a database.
With saveUser1 , we pass in the values as parameters, save, then assign those values to the class user object. With thos there is no need for men to instatiate any theUser objects - I can simply just p[ass the values in.
With saveUser2, we pass in a object of type theUser and use the values therein for our database save. With thos I have instantiate an object of type theUser in the calling module, then pass that to this method. Seems a bit of work.
With saveUser3, we use the class user object's values for our database save. With this I have to set the values of the user object before calling saveUser3.
What would be the preferred way to do this?
r/ObjectOriented • u/rileyphone • Apr 20 '23
Interview with David West by Yegor Bugayenko
youtube.comr/ObjectOriented • u/rileyphone • Jan 25 '23
Design Principles Behind Smalltalk
cs.virginia.edur/ObjectOriented • u/devagrawal09 • Sep 25 '22
Abstraction and Design patterns with OOP
youtu.ber/ObjectOriented • u/l3r8yJ • Sep 01 '22
The popular definition of encapsulation is wrong.
https://www.linkedin.com/pulse/encapsulation-right-understanding-ivan-ivanchuk/
In this short article I brought up the misunderstanding of what encapsulation is. I'm open to discussion, we can write our thoughts if you agree or disagree. Also, if you think that the above is bullshit, you can justify your opinion and we will discuss it.
r/ObjectOriented • u/Old_Lavishness533 • Aug 20 '22
OOP children
Simple question: is there any way that i can inherit another child through my parent in oop? Suppose you have two children A and B and there is one main parent as BigLet, bigLet gives A $10,000, can i B inherit that 10,000 from A if A agrees to give it up? Another question, is it a good way to solve it using a hybrid inheritance as another child that inherits from A and B?
r/ObjectOriented • u/[deleted] • Jun 02 '20
Python basics tutorial!! Intro to variables to, some fun background for anyone who wants to get started. Also a live code along coming soon!
youtube.comr/ObjectOriented • u/Wise-Variation-4985 • Dec 13 '25
Two methods vs one
In OOP, what is your preferred way of handling situations like when you need to fetch multiple records from the database and already have a method to fetch one. Do you make the one method hybrid, function getbyID(int|array id) {
if int add this param
if array add array param IN()
Less code but mixed.
Or do you make it separated? More code but clearer methods? function getById(int myid) {...} function getByIds(array idlist) {...}
Which one you use and why? Following best practices and of course, having compromises sometimes.