Solved

object oriented programming real examples

Posted on 2013-10-22
8
336 Views
Last Modified: 2013-10-29
I am looking for real world explanations of object oriented programming


A student is an instance of a class
a house is an instance of a housing community
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39592290
No, they are not instances - an instance is a 'concrete' object. They are classes and subclasses in the hierarchy.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39592323
I would say that a class is a collection of students, and a housing community is a collection of houses. Or in C# terms:

public class Studen
{

}

public class Class : ICollection<Student>
{

}

public class House
{

}

public class HousingCommunity : ICollection<House>
{

}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39592350
Now, to get more object-oriented, let's extend the house route. A house is a building, yes?

e.g.

public abstract class Building
{

}

public class House : Building
{

}

Open in new window


But a skyscraper is also a building, correct?

public class SkySkraper : Building
{

}

Open in new window


What would you consider a city block to be? I think of it as a collection of buildings:

public class CityBlock : ICollection<Building>
{
    private List<Building> myBuildings = new List<Building>();

    public void Add(Building item)
    {
        this.myBuildings.Add(item);
    }

    // Remaining ICollection methods
}

Open in new window


Since it contains Buildings, then theoretically it could have both houses and skyscrapers, right?

static void Main(string[] args)
{
    CityBlock block = new CityBlock();

    block.Add(new House());
    block.Add(new SkyScraper());
}

Open in new window


One part of object-oriented programming deals with polymorphism--which what I believe you are asking about. Polymorphism is treating an object like it is something else under different contexts. Above, I created a new House and a new SkyScraper (two different kinds of objects, I'm sure you'll agree), but when I added them to the block I treated them as Buildings. I could do this because both the House and the SkySkraper inherit the Building base class.

Polymorphism is a bit more broad than what I've described above. Digest the above first, then we can follow up with remaining questions you have  = )
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 16

Expert Comment

by:krakatoa
ID: 39592388
kaufmed - pity no Java there. ;)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39592393
My Java kung-fu is not that strong, so I didn't want to embarrass myself  ; )
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39592403
But it might have embarrassed me. Or helped me, more likely. ;)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39592421
(Sorry, deleted this comment - too involved.).
0
 

Author Closing Comment

by:rgb192
ID: 39610827
Good example of complex usage.

Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question