5 Replies - 2490 Views - Last Post: 21 October 2014 - 01:18 PM Rate Topic: -----

#1 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 20 October 2014 - 01:19 PM

Hi Guys

So i have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.

Or do these concepts just not work with WPF programs?

Is This A Good Question/Topic? 0
  • +

Replies To: How do i use encapsulation, polymorphism and inheritance in C# WPF

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 20 October 2014 - 01:37 PM

They absolutely work in WPF. Those concepts are about OOP and not at all specific to any language. They are not even C# speocific: Any OOP language makes use of these concepts to some degree.

Remember that WPF is just about how the UI is built and interacted with. It replaces WinForms by providing a UI technology that is DECADES newer/better than WinForms. But underneath it all is that same C# that you've dealt with all along.

You are finding info about how these concepts relate to WPF because they don't relate specifically to any language or UI building technology.

Its like trying to find info on "How does a hammer relate to building a 9 story building?"
Well, it doesn't relate to that specific situation any more or less than it does to any other project.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 20 October 2014 - 02:08 PM

tlhIn`toq is correct. These are not patterns specific to any one language. The principles you're describing are actually principles of Object Oriented Programming, not any one particular Object Oriented Language.

Actually, having re-read this line, I have something else to say:

Quote

'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts...


That's not necessarily true. They're "concepts" or "principles" of object oriented programming, but that doesn't mean that you have to apply them to every single program you write. I don't always use inheritance directly in my programs. And "encapsulation" is so broad that you can't really say "I'm using/not using encapsulation", because it's a description of wrapping and hiding internal functionality inside a component so that it doesn't leak it's details.

It might help to simplify a few of these concepts. We could link tons of articles explaining these concepts in depth, but they can all be summed up in a single sentence each:

  • Polymorphism: the ability to treat an object of one type as an one of another related type.
  • Inheritance: the ability to share functionality of a "parent type" by creating a type derived from the parent
  • Encapsulation: packaging data and functionality into an entity that manages it's own internal workings.


It's that simple. You can apply these concepts broadly. You can even apply these design principles when dealing with a non-object oriented language; you just might not have language features helping you out.

.NET in general relies heavily on all these principles. Almost every class you use in your programs that come from the .NET Framework are inherited from some other type. Even classes that don't inherit from anything actually inherit from System.Object, and thus use polymorphic and inheritance concepts. And classes themselves are an implementation of the encapsulation principle. So they apply at a lower (or higher, depending on your viewpoint) level than a presentation framework like WPF do.
Was This Post Helpful? 2
  • +
  • -

#4 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 21 October 2014 - 05:12 AM

In fact, even in using .NET and WPF you can't avoid using these concepts. All .NET framework objects inherit. All use encapsulation. All use polymorphism. Even if you're not directly using these concepts in your design, you are at the very least using objects that do.

I wonder if your question is more aimed at XAML: well, you might not realise this, but all the XAML does is instantiate .NET objects and renders them at certain points. It is not quite as obvious at first glance though. Even when you create a page, you are writing a class that inherits from Page, then you are instantiating it when used. It's different syntax, but certainly relies on OOP concepts.
Was This Post Helpful? 0
  • +
  • -

#5 danbywinby   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 305
  • Joined: 06-January 13

Re: How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 21 October 2014 - 01:05 PM

So would i be correct then in saying the below:

My program uses 'Encapsulation' by encapsulating all the events for my 'Window1' in the 'Window1.xaml.cs' file.

My program uses 'Inheritance' by having 'UserControl_1' inside 'Window1' and so it inherits data from 'Window1'.

If so then i am amazed because i though these concepts were related the actual code and not just how you make the program function or design it to function.

But i still don't quite understand 'Polymorphism' or is that like saying i have two text boxes which use 'Polymorphism' because they are both text boxes.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How do i use encapsulation, polymorphism and inheritance in C# WPF

Posted 21 October 2014 - 01:18 PM

No. Just because your UserControl is in a Window does not mean it inherts from the Window class. Same problem with encapsulation: Just because the events are in the Window code does not mean the window encapsulates them.

Clearly you don't understand these concepts. I have my doubts if you've worked any tutorials on the subject.

There is a tutorial on objects/classes linked in my signature block that should help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1