Click here to Skip to main content
15,884,001 members
Everything / OOP

OOP

OOP

Great Reads

by Afzaal Ahmad Zeeshan
This post attempts to describe the general difference between overloading and overriding in Object-oriented programming languages.
by Giovanni Scerra
Patterns to prevent null reference exceptions
by Giovanni Scerra
An introduction to transparency in software design, with particular focus on OO languages
by Mahmoud Samir Fayed
Defining Natural Languages in the Ring programming language based on Object-Oriented Programming

Latest Articles

by Yochai Timmer
A way to avoid JNI's reflection oriented programming. Wrapping Java with C++
by Alex Rakov, Alexandre Bencz
ELENA is a general-purpose, object-oriented, polymorphic language with late binding
by Bohdan Stupak
A look at what dynamic polymorphism is and how it is crucial to mastering OOP
by Bohdan Stupak
"Method can be static" Microsoft Code Analysis warning may hide a violation of object-oriented design

All Articles

Sort by Score

OOP 

4 Sep 2015 by Afzaal Ahmad Zeeshan
This post attempts to describe the general difference between overloading and overriding in Object-oriented programming languages.
26 Feb 2015 by Giovanni Scerra
Patterns to prevent null reference exceptions
2 May 2015 by Giovanni Scerra
An introduction to transparency in software design, with particular focus on OO languages
11 Dec 2011 by Roger Wright
There are a number of excellent answers here, but let me toss in another point of view, if I may. The jargon in this industry is a good example of data hiding, in many cases, and being a simple person of simple mind, perhaps I can simplify.First, Abstraction is simply separating the top...
12 Oct 2016 by Mahmoud Samir Fayed
Defining Natural Languages in the Ring programming language based on Object-Oriented Programming
7 Jun 2016 by GProssliner
cobj is a preprocessor based generator for interface based polymorphism
10 Apr 2018 by Nick Polyak
Achieving separation of concerns using Roxy IoC Container and Code Generator.
27 Jul 2013 by Giovanni Scerra
Inheritance and code reuse
16 Aug 2011 by Damian Flynn
Here's a couple of gems...From IList to DataTable.From DataTable to array of T.// DataTable: from IListpublic static DataTable ToDataTable(this IList iList){ DataTable dataTable = new DataTable(); PropertyDescriptorCollection propertyDescriptorCollection = ...
5 Nov 2011 by Sergey Alexandrovich Kryukov
Well, do you have a hobby? Anything not related to programming but where a program or a Web resource could help. Music, arts, crafts? Sports, books, video, collections, playing with children or teaching them? Are you learning yourself — drawing, painting, playing a musical...
29 Dec 2011 by #realJSOP
There is no real order of importance. To code in .Net, you have to understand the framework.The key isn't learning everything - the key is learning everything you need to learn, as soon as the requirement presents itself. Pretty soon, you'll know a lot about everything. In the biz, we call...
20 Mar 2019 by Matías Fidemraizer
Discover a modern, solid, and powerful way of leveraging object-oriented programming on the Web and anywhere
13 Feb 2018 by Borislav Stanimirov
A new take on polymorphism in an object-oriented setting
10 Apr 2011 by Sergey Alexandrovich Kryukov
It is not needed — not at all. Function overloading is only for convenience, just to allow same name for actually different methods. Compiler can tell one method from another just by looking at the parameters of the call, where possible.Formally, I answered this question fully....
19 May 2012 by Sergey Alexandrovich Kryukov
I see no problem here at all. The interfaces may inherit one from another or not. A class or a structure can implement either of the two or both of them. How exactly you build this hierarchy depends on the application of all those types and does not depend on signatures of the methods you show...
21 Aug 2012 by Rahul Rajat Singh
refer these:run-time polymorphism VS compile-time polymorphism[^]http://stackoverflow.com/questions/673721/overloading-and-overriding[^]
18 Apr 2022 by Bohdan Stupak
A short take on brittle unit-tests and how to avoid them
30 Mar 2011 by Sergey Alexandrovich Kryukov
It looks like you badly misuse header files. Also, you mess up unrelated concepts: separate compilation, access of instances and memory allocation.It is very bad idea to put any objects in a header file. Put only types and method declarations (and some inline definitions, see below). Just...
10 Apr 2011 by santosh dhanawade
Hey dear first try google for this then asked questionGoogle[^]but let me explain some logic and some interesting concept,A Function overloading means same function with different parameter list like,following add(int k,int j) //for adding two integeradd(float k,float j)//for...
9 Sep 2011 by Wendelius
If I understood your questions correctly:First of all the list should be defined at module level (at least). If you define the list in a click event handler, you cannot access it in another method. So at module level something like:public partial class Window1 : Window { private List...
5 Nov 2011 by RaisKazi
You may have a look at many Question-Answer discussions on this topic posted on CodeProject earlier.http://www.codeproject.com/search.aspx?q=c%2B%2B+oop+project+ideas&sbo=qa&usfc=false&x=10&y=11
24 Jul 2013 by Ron Beyer
The answer is A and B...Classes can have constructors and destructors. (If you can construct it, you need to be able to destroy it)Classes are reference types (value types are like int, long, short, etc)Classes DO support inheritance, its a fundamental concept of OOP.I'm not sure...
25 Sep 2017 by OriginalGriff
It's a bit complicated, but I'll try to explain... Remember that a class can derive from a single base class (concrete or abstract), but can also inherit multiple interfaces. So descendant classes can implement interfaces that the original base class did not. class MyBase{} class MyDerived :...
23 Feb 2022 by Michael Sydney Balloni
Recycle objects that are expensive to create and improve the performance of your application
3 Feb 2011 by sam, atlanta
I was asked this question in an interview, but could not figure out the answer. I did some google too, but maybe was not in a proper direction. Maybe someone at codeproject can help...String s = "Codeproject";String s = new String();what is the difference in these two...
7 Apr 2011 by #realJSOP
There is no "better" way to do it. Here are my personal rules0) If you are bound by coding guidelines relating to constructors, follow them. Period.Otherwise...1) If you are editing an existing file, follow the coding style used in the file2) If you are creating a new file, do...
7 Apr 2011 by Sergey Alexandrovich Kryukov
1) This is can be used as an accessor; you are right. If you need to modify the field of the already constructed class instance, what else can we do? We can use this accessor or side effect from some (non-const) instance (non-static) function called on this instance.2) This form of writing...
9 Apr 2011 by AspDotNetDev
There are many different uses for the constructs you've described, and it would be much easier to choose between them given a usage scenario. However, one thing I will note is that your third item is making use of an initialization list.Initialization lists can be used to pick which parent...
20 May 2011 by Not Active
class b : a{ public override string add() { return base.add(); }}
13 Aug 2011 by Vano Maisuradze
Very nice!I've modified ToCollection(this DataTable dt) extension function, so if DataTable column names and class property names are different, then you can use this alternative:public static List ToCollection(this DataTable table, Dictionary dic){ List lst = new...
17 Oct 2011 by Prerak Patel
1st one is just the declaration of variable. It is not instantiated. When you run it, you get 'Object reference is not set' or such error. 2nd one shows how to instantiate. It creates an object of classA and assigns it to objB.Learn more by books/google.
20 Oct 2011 by RaisKazi
You are mixing up two different terms here.1) Type2) Access ModifierType of "String st;" is System.String.Default access modifier of variable/object is private.
21 Oct 2011 by Abhinav S
Encapsulation is essentially "data hiding".A class is a construct that can be instantiated to create objects in memory. The class will use encapsulation (e.g. public and private methods) to hide attributes that you don't want to allow public access to. Finally an instance of the class will...
21 Feb 2012 by TRK3
Just because the language doesn't provide a means of protection / encapsulation, doesn't mean you should abandon accessors.I learned OOP and the importance of encapsulation directly from Barbara Liskov.My first professional programming job after that was in C. Even though we were writing...
9 Apr 2013 by Sergey Alexandrovich Kryukov
No. And you can easily check it up by writing code and trying to compile it. The indication that a class is abstract with the use of the keyword "abstract" is a sort of a fool-proof feature. It's pretty much the like the absence of non-declared variables.You can define an abstract class with...
7 May 2013 by lukeer
Quote:And how can it be solved in languages like C# or Java in which global stuff is forbidden ?You create a static class and put the static method there.So it's accessible from wherever you need it.
19 May 2013 by OriginalGriff
You can't.XML is a language similar to HTML used for creating structured documents and web pages.C++ is a general purpose object oriented programming langusge.You cannot comfortably store information in C++; you cannot process information or write an application in XML.See here:...
17 Jun 2013 by Shubhashish_Mandal
In Java, to access any properties of a given class, you have to create the object first.Therefore if you want to access the properties of class Basics inside class AccessVaria , then you have to create a instance of class Basics inside AccessVaria.For more information click here."..Do...
20 Aug 2013 by OriginalGriff
I'd go with the first way, or create a constructor:public A(string name, string address) { Name = name; Address = address; }...A objbA = new objA("abc", "xyz");objA.Save();
24 Aug 2015 by Maciej Los
In addition to solution 1 by Sergey Alexandrovich Kryukov[^], i'd suggest to read this: CA1402: Avoid overloads in COM visible interfaces[^]. It might help you to understand what Sergey wanted to tell you.In a short: you have to go back to basics and read about inheritance[^],...
2 Jun 2016 by OriginalGriff
Generally speaking, you don't - it's a poor idea from an OOPs point of view in that it "locks" the two classes together.Instead, you should create an event in your Class1 which the Form class handles. The form calls it's own method, and returns any results to the class1 instance via a property...
13 Oct 2016 by Rahul Rajat Singh
This is a small tip containing my thoughts on why we should not blindly create interfaces and/or abstract class for each and every class in our application.
2 Jul 2017 by F-ES Sitecore
First of all you have to understand reference types in general. You have a Circle object somewhere in memory and you have two variables pointing to it, or referencing it; one is "c" and one is "s". If you update a variable via "c" then "s" will see the same as there is only one actual object...
3 Apr 2018 by OriginalGriff
Any cast - implicit or explicit - changes the type of an object: but it doesn't actually change the object at all, it just checks that the object is of a type suitable to cast, and that such a cast operation exists. For example, explicit cast operators exist for int to decimal, double to float,...
2 Sep 2019 by Afzaal Ahmad Zeeshan
Quote: as you guys can see class `Employee` extends `Person` thus it will have all it's fields and method right ? Only if you will do, Employee e = (Employee)p; Then, after a cast, you can see Java being printed. Currently, there is no connection between what p is having and what e contains....
12 Sep 2021 by CPallini
Quote: I'm wondering where is the object of the (*age) member in the copy constructor body? Quote: I've tried to debug and see locals to see what's going behind I see a pointer variable called this is being created when the constructor is called...
31 Dec 2021 by Richard MacCutchan
You could probably simplify that by the use of the singleton pattern - Google Search[^].
5 Jan 2022 by Stefan_Lang
You seem to try and imprint another languages' properties onto C++, and that is never a good idea! My experience with java is limited, and I'm not sure what you actually have in mind, but the code you posted is clearly not the recommended way of...
10 Jan 2011 by XTAL256
I am writing a C++ application where i have an object model and a UI (as most applications do). For the model, i have a main object and a number of specialised subclasses. The UI has context menus and property dialogs that i want to be specific to each object type. The main object has a number...
7 Feb 2011 by Cent@urus
Both have their use and it is up to you to decide when to use which.I use Interfaces practically all the time to define the contracts my componets will support or will give support to. Sometimes I myself implement those interfaces, sometimes it is somebody else. When is the case I create one...
10 Apr 2011 by OriginalGriff
To make life easier for us all.If I construct a generic email method, I will construct it with a number of overloads: public static void Email(string body) public static void Email(string body, params MailAttachment[] attachments) ...
11 May 2011 by OriginalGriff
That is exactly why the return type is not used to discriminate between overlaods - you will get a compiler error if you try.Think about it - assume you have two overloaded methods: public HSObject Add(int a, int b) { ... } public HSSlide Add(int a, int b) { ... }It looks...
11 May 2011 by Sergey Alexandrovich Kryukov
It has nothing to do with OOP!Your example will not compile:int Add(int a, int b) { return a + b; }string Add(int a, int b) { return (a + b).ToString(); }Error message:Error 1 Type 'ContraOrCovariance.Program' already defines a member called 'Add' with the same parameter...
10 Jul 2011 by Manfred Rudolf Bihy
It's by design. Static methods have their merit in cases when functionality is independant to instance variables. Also it's important for program entry points like main in applications. Cheers!—MRB
9 Sep 2011 by Wayne Gaylard
OK, to add cars to your List double noCylinders; double topSpeed; double horsePower; double.TryParse(textBox2.text,out noCylinders); double.TryParse(textBox3.text,out topSpeed); ...
9 Sep 2011 by Simon Bang Terkildsen
First of I don't feel it's a good idea to use WPF while learning how to program, so I suggest you forget about WPF and start doing some basic console applications. trying to learn WPF while is not a good idea, I know good programmers who have had trouble learning WPF or think they have learned...
9 Sep 2011 by BillWoodruff
I think your "fully lubed" in great advice here :), but let me add a few comments, at the risk of oil-pan overflow:0. what seems 'missing' in the structure of your list is some ID for each Car: what if you have four cars that all have the same identical specs ?1. replace the TextBoxes...
9 Dec 2011 by #realJSOP
When you want to add different or additional functionality with the same method call. A good example would be ToString(). When you can a collection of objects that you want to display in a listbox (or other list container), you can simply override the Object.ToString() method to return the...
29 Dec 2011 by Wendelius
This depends greatly on what your focus is. Based on the info provided you are focusing on data operations. If that's the case LINQ and WCF are strong candidates along with for example EF (Entity Framework). On the other hand if you want also to learn about UI specifics, perhaps ASP.NET or WPF...
2 Jan 2012 by Richard MacCutchan
See here[^].
21 Feb 2012 by Chrene91
I have programmed for about 2-3 years now and actually I am developing a game in actionscript atm. I have a character class which have some fields like name, level, experience, damage, defence and so on. right now I have made them protected so the deriving classes can see them, and I have used...
22 Feb 2012 by Sergey Alexandrovich Kryukov
Almost everything is explained by Edward in his comment to the question. I will add just a bit.This is a pretty interesting discussion of the rationale for different access modifiers:http://www.actionscript.org/forums/archive/index.php3/t-113383.html[^].Pay attention for description of...
25 Feb 2012 by OriginalGriff
Which would you rather remember: one method name, with 6 overloads, or 6 different method...
25 Feb 2012 by Sander Rossel
Well, one obvious advantage I can think of is that you don't have to think for a name for every overload...For example, MessageBox.Show in .NET has 21(!) overloads. Imagine having 21 methods that all do the same...
10 May 2012 by ahp-1984
Hi, I am asp.net programmer.i have read the articles of abstract class and interface and i know how to implement it also.but can anybody explain me practically i.e. by referring project scenario where to use abstract class and where to use interface? I need a very practical or real...
10 May 2012 by Sandeep Mewara
Inheritance: Exposes the managed object as the outer objectContainment: Enables the outer object to modify the behavior of the inner object.Here have a look at how both are used differently in the following scenario: MSDN: COM reusability in the .NET Framework[^]
17 May 2012 by Sander Rossel
Hi all,I'm having a little problem designing an Interface I have (not the GUI).What I basically have is two Interfaces that do the same, but one of them needs to get an extra parameter to a Function.Public Interface IDoSomething(Of T) Function DoSomething(ByVal something As T) As...
21 Aug 2012 by Sunil Kumar Pandab
Of course Yes dear, Both overloading and overriding achieve polymorphism. And even in c# there is another concept introduced with them, that is Method Hiding.static polymorphism :- overloading dynamic polymorphism :- overriding and Method Hiding
21 Aug 2012 by CPallini
Abstract classes are mainly used for specify interfaces (interface is a basic OOP concept, make sure to grasp it).You transform a concrete class into an abstract one simply adding to it a pure virtual method (or changing an existing method to pure virtual), e.g.// 'Concrete' Fooclass...
21 Aug 2012 by Legor
An abstract class in C++ contains at least one pure virtual function which is a function defined as:virtual void foo() = 0;.The important part here is the "= 0" statement.If you inherit from an abstract class you'll have to provide function definitions for every inherited pure virtual...
12 Oct 2012 by CPallini
Anonymous classes are allowed (see, for instance, MSDN[^]). I think they are not a loophole.
12 Oct 2012 by armagedescu
You may declare instances of that class.class{public: int x;} xaaa1, xbbb1, xccc1;class{public: int y;} xaaa2, xbbb2, xccc2;int main(){ xaaa1.x = 1; xaaa2.y = 2; xbbb2.y = 3; cout
9 Dec 2012 by Sergey Alexandrovich Kryukov
You need to look at Data Contract. Please see:http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].Please see my past answer where I explain it:How can I utilize XML File streamwriter and reader in my form application?[^],Creating property files...[^].—SA
12 Dec 2012 by Sergey Alexandrovich Kryukov
Please see my comments on persistence. If should be either Data Contract or something like that. It should be agnostic to UML. As soon as your persistence start depending on UML definitions or anything of application field, consider all your development dead. And it should be no XAML, because...
12 Dec 2012 by Sergey Alexandrovich Kryukov
OK, I'll show some very rough sketch of UML hierarchy. Very preliminary, pseudo-code, and only for Class Diagram and top level. Perhaps you well see that you might under-estimate the task.Let's start with abstract top level of hierarchy: abstract class Element { // Name:...
2 Feb 2013 by Abhinav S
Just a naming thing.Instances of a class are created and these instances are the objects that will be worked with.
21 Mar 2013 by Sergey Alexandrovich Kryukov
OK, interviewers can be idiots. Well too often, it sounds like. It does not mean that we should explain every case of idiocy. As to this particular case, I'm not quite sure the question was idiotic. Perhaps they told you a bit more. Perhaps you were expected to ask for clarification; and they...
18 Jun 2013 by Prasad_Kulkarni
OOPS Concept with Real-world example[^]OOPS Concept With Real Life Example[^]..and 913,000 similar threads here[^]
16 Jul 2013 by Bernhard Hiller
When I started working with VB6, I favored "modules" over "classes", and that's somehow similar, with modules corresponding to "static". The paradigm shift to classes took me some time. An important point was that I can have multiple instances of a class which do not interfere with each other:...
25 Jul 2013 by Sergey Alexandrovich Kryukov
Please see my comment to the question.You see, as you don't have a right idea on how much reading you would need to get these concepts, chances are, you need to start pretty much from the very beginning. You need to grab some textbook/manual/tutorial on .NET and C# programming (most of it is...
27 Jul 2013 by pasztorpisti
Regardless of the actual implementation/class you instantiate put the result pointer into a base class pointer and all the general implementation independent code should work with base class pointers/references.BaseClassPointer* GetInstance(){ if (globalsettings == "whatever") ...
14 Aug 2013 by Sisir Patro
Hi,Refer the following links:Find memory leaks and optimize memory usage in your .NET application[^]Memory Management: Examples[^]3.9 Automatic memory management[^]Releasing Memory taken by variables[^]Thanks
14 Aug 2013 by Sushil Mate
Why don't you start from this link?Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework[^]Then try reading 1) Dot net memory management basics.[^]2) Memory Management in .NET[^]3) Memory Management Misconceptions[^]Hope this helps :)
20 Aug 2013 by CPallini
The second method has redundance and should be avoided.Both first and second method allow a possibly 'uninitialised' object to be saved.I would define a constructor the way OriginalGriff suggested, in order to put the object in a initialized state on its very creation.
14 Feb 2014 by Sergey Alexandrovich Kryukov
No, not the same. In essence, the answer can be found in my comment to the question.I think it would be enough if I give you some hint withing the scope of C only. You are not yet ready for OOP and need to understand some more fundamental notions before jumping to OOP. Even in C, object and...
27 Feb 2014 by OriginalGriff
Yes, or you can hide it using newhttp://msdn.microsoft.com/en-us/library/ebca9ah3.aspx[^]
25 Jun 2014 by OriginalGriff
Pretty simple:A private constructor allows you to create a class that can't be instantiated from outside the class: the outside world can't do this:MyClass mc = new MyClass();The only way to create an instance is to call a static method of the class and have it return the instance.This is...
22 Aug 2014 by PIEBALDconsult
A simple search here on Code Project finds these and many more articles:Why do we need Interfaces?[^]Interfaces and its Importance[^]Demystifying interfaces in C#[^]
4 Nov 2014 by Maciej Los
Please, start with MSDN documentation:Static Class Design[^]Static Classes and Static Class Members (C# Programming Guide)[^]
17 Nov 2014 by Sergey Alexandrovich Kryukov
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown. Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its...
17 Nov 2014 by BillWoodruff
Your error is caused by trying to use 'Trim on an uninitialized (null) string.
21 Nov 2014 by OriginalGriff
"without using any dummy classes" eh? OK...Look at the standard ToString method:It is implemented by the object class (from which all the other classes are derived) and it returns a string which is the full name of the object.Most (but not all) derived classes override this to provide a...
10 Mar 2015 by phil.o
We do not do your homework. It has been given to you for good reasons: it will allow you to learn something. Trying to have this done by someone else defeats its purpose.Plus, stating your urgency is not going to help, either; it is considered as rude, and it only tells us that you cannot...
20 Mar 2015 by OriginalGriff
No, it won't. a web application is not like a Windows application: it doesn't run all the time waiting for the user to do something.Page_Load occurs every time that the user does anything which "talk" to the server: it doesn't occur once and then never again until the user changes pages....
21 Jul 2015 by F-ES Sitecore
If you can't answer these questions then you're not fit for even a basic developer role. If we give you the answers to these questions then are you just going to hope they come up at the next interview? If they don't will you then ask here for the answers to the new questions you didn't know? ...
17 Feb 2016 by Richard MacCutchan
No and it will never work because you have made all the properties static, so they apply to every instance of child. Remove the static keyword from all those properties, and then you can create any number of objects like:child Robert = new child();Robert.name = "Robert";// ...child...
2 Jun 2016 by Richard Deeming
Don't call the Speak method until the window's Loaded event fires:public abc(){ InitializeComponent(); qwe = new SpeechSynthesizer(); Loaded += Window_Loaded;}private void Window_Loaded(object sender, RoutedEventArgs e){ qwe.Speak("Hello");}If that's still...
2 Jun 2016 by VR Karthikeyan
Hi, this is simple. private void button1_Click(object sender, EventArgs e) { Class1 sample = new Class1(); sample.send(); }In this above block of code, you are creating sample object using Class1's default constructor new Class1();,...
21 Jul 2016 by Bernhard Hiller
Quote:and i need to pass body as parameter to another methodThat other method needs to be generic, too; or at least accept a type compatible with T. Otherwise, the encapsulation in the generic class does not make sense.