Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

object oriented programming real examples

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
rgb192
Asked:
rgb192
  • 4
  • 3
1 Solution
 
krakatoaCommented:
No, they are not instances - an instance is a 'concrete' object. They are classes and subclasses in the hierarchy.
0
 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
krakatoaCommented:
kaufmed - pity no Java there. ;)
0
 
käµfm³d 👽Commented:
My Java kung-fu is not that strong, so I didn't want to embarrass myself  ; )
0
 
krakatoaCommented:
But it might have embarrassed me. Or helped me, more likely. ;)
0
 
krakatoaCommented:
(Sorry, deleted this comment - too involved.).
0
 
rgb192Author Commented:
Good example of complex usage.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now