Inheritance and abstraction

by iatanasov 14. May 2007 06:20

Inheritance and abstraction are also very important features of object-oriented languages. They provide a way to make polymorphic representations of objects and object relationships that can be managed at run time or compile time.

 

Inheritance is the ability of one object to be derived by creating a new class instance from a parent or base class and overloading the constructor(s), methods, and attributes of that parent object and implementing them in the instance. In Java this is known as subclassing. Inheritance is important because many times an object contains some base functionality that another object also needs and, instead of maintaining the same logic in two objects, they can share and even override or change this functionality by using a base or parent class. If this occurs, then the base or parent object should be defined in such a way that several common derived objects can use the same common functionality from the parent. The parent should only contain functionality common to all its children.

Abstraction is the actual method in which we use inheritance. Abstraction is the ability to abstract into a base class some common functionality or design that is common to several implementation or instanced classes. The difference between implementation and abstract classes is that abstractions of classes cannot be instanced, while implementations can. Abstraction and inheritance are both aspects of polymorphism, and the reverse is true as well.


Another important aspect of object-oriented languages is how they deal with collections of objects. The equals implementation for objects is an important aspect of dealing with objects inside a collection. Languages like C#, VB.NET, and Java all use this method to help index and compare objects in collections. Let’s talk about this briefly. When a hash table or other collection object indexes and compares an object, it uses the GetHashCode() method to help in this indexing and comparison. This method can be overridden to capture a more accurate sampling of the intrinsic properties or state of the object. In other words, the GetHashCode() method can return an integer representation of the concatenated state of the properties within an object. If not overridden, then this relationship is less exact. This is important when making comparisons between objects in collection classes like iterators or generic collection objects like hash tables. You need to make accurate representations of the internal state of objects so the correct object can be compared or indexed in a collection.

There are general rules to guarantee that each object gets a

unique hashing algorithm: Objects that compare as equal must return the same hashed value. GetHashCode() must return the same value every time, unless the internal value or state is modified. The hashed value is not like a GUID (global unique identifier) in that it is not globally unique, but only unique if the hashed algorithm and the object’s value are not the same as any other object in the scope of the executing code.

The default implementation of GetHashCode() in objects that contain state variables or values is not guaranteed to be unique. That is why if uniqueness is desired, then a proper algorithm needs to be implemented in the overridden method on a particular class. To provide a complete representation of state, the values of each variable that represents the object’s state need to be part of the hashing algorithm. To illustrate the proper way to implement the GetHashCode() method, take a look at this example:


public override int GetHashCode()

{

return _name.GetHashCode() ^ _address.GetHashCode();

}


We see that the method has been overridden, and two instance variables have been concatenated with the ^ symbol and returned as their sum. Another way to do this is with the + sign, which returns the same result:


public override int GetHashCode()

{

return _name.GetHashCode() + _address.GetHashCode();

}



Taking all the variables that may change the state of the object and returning their concatenated hashed values guarantees that each object will have unique values based on state. This allows objects used as keys in collections like hash tables to act in the proper manner. The Equals(object obj) method is the required method for bitwise comparisons of value objects. It is especially useful in sorting collections or when a comparison operation is desired in a collection. Not all primitive or object types can have bitwise equality, and so those are compared by value, as in the case of decimal 2.2000 and 2.2, which have the same value but different binary equality. The proper operational sequence usually starts with a null check, then a class type comparison, and then a comparison of all the value types (or reference types) that influence the state of the class:


public override bool Equals(object obj)

{

if(obj != null && obj is Component)

return _name.Equals(((Component)obj).Name) &&

_address.Equals(((Component)obj).Address);

else

return false;

}



There are some basic rules when testing the equals implementation for proper return values:

obj1.Equals(obj1) = true — an object always

equals itself.

obj1.Equals(obj2) = obj2.Equals(obj1) —

equals implementations across different class instances

always return true on both classes if equal.

obj1.Equals(obj2) && obj2.Equals(obj3) && obj3.Equals(obj1) = true — if object 1 is

equal to object 2 and object 2 is equal to object 3, then

object 3 must be equal to object 1.

All calls to Equals() return the same value unless the class’s state or internal value is modified.

Equals(null) always returns false.


 

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

object-oriented programming

Related posts

Comments

February 25. 2010 17:34

Gravatar

I just hope to have understood this the way it was meant

paydayloans

February 27. 2010 08:16

Gravatar

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

cash advance

February 28. 2010 12:11

Gravatar

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

Acne products

March 3. 2010 23:50

Gravatar

There are certainly a lot of details like that to take into consideration.

Colon cleansers

March 4. 2010 13:48

Gravatar

Your blog is so informative … keep up the good work!!!! Big thanks for the useful info i found on Inheritance and abstraction.

instant loans

March 7. 2010 04:05

Gravatar

I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Big thanks for the useful info i found on Inheritance and abstraction.

payday cash advance

March 8. 2010 20:52

Gravatar

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article. Big thanks for the useful info i found on Inheritance and abstraction.

payday cash loans

March 11. 2010 02:50

Gravatar

Interesting post and I really like your take on the issue. I now have a clear idea on what this matter is all about. Thank you so much.

arac sorgulama

March 13. 2010 16:24

Gravatar

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

Stretch marks

March 26. 2010 11:41

Gravatar

I enjoying this site, this site some great resource that you http://delicious.com/ofself are providing and give it away for free. Thanks http://faves.com/users/thigpenmorgan for taking the time to discuss this site. I really loved reading this post.

Ailene Tanen cn

March 26. 2010 12:01

Gravatar

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

Theressa Cronoble cn

March 26. 2010 12:27

Gravatar

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

Marianela Cilenti cn

March 26. 2010 13:36

Gravatar

I would like to http://www.extratasty.com/profile/23730/richard56 add your blog to my blogroll http://kaseyluvblog.blogspot.com/2010/03/drinking-water-filter-system-review.html please tell me what anchor should I use?

Monte Bocock cn

March 26. 2010 23:34

Gravatar

This is exactly what i was looking for. thank you for the informative post and keep up the good work! Big thanks for the useful info i found on Inheritance and abstraction.

toenail fungus remedies

March 28. 2010 05:00

Gravatar

good good…this post deserves nothing :( …hahaha just joking :P …nice post :P

quick personal loans

March 30. 2010 13:04

Gravatar

Happy to see your website! http://www.slideshare.net/jkljkl520/drinking-water-filter wish you have a great day! http://www.scribd.com/doc/29082722/Drinking-Water-Filter Thank you for this blogging http://www.docstoc.com/docs/31977803/Drinking-water-filter I will bookmark again.

Dorthey Megee cn

March 31. 2010 06:51

Gravatar

As a Newbie, I am always searching online for articles that can help me. Big thanks for the useful info i found on Inheritance and abstraction.

hpv genital warts

April 2. 2010 11:31

Gravatar

Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.

used car finance

April 3. 2010 18:06

Gravatar

That is some inspirational Never knew that opinions could be this http://www.blogigo.com/changemylife/need-say-somthing-about-Aquasana-drinking-water-filter/10/ varied. here.

Gino Liebross cn

April 5. 2010 13:30

Gravatar

Hello, I found your blog in a new directory of blogs. I dont know how your blog came up, must have been a typo, Your blog looks good. Have a nice day.

help with credit card debt

April 6. 2010 04:20

Gravatar

I completely agree with the above comment, the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly. Big thanks for the useful info i found on Inheritance and abstraction.

cellulite treatment us

April 6. 2010 20:07

Gravatar

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

pork casserole recipes cn

April 7. 2010 08:14

Gravatar

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

chocolate ice cream recipes cn

April 9. 2010 02:53

Gravatar

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

water filter drinking us

April 11. 2010 18:41

Gravatar

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

easy cash loans us

April 25. 2010 09:56

Gravatar

e

laptop cases us

April 29. 2010 03:05

Gravatar

I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained!

Insurance Litigation Attorneys us

May 3. 2010 07:52

Gravatar

That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

long term personal loans us

Comments are closed

Powered by BlogEngine.NET 1.1.0.7
Theme by Mads Kristensen

About the author

Ivan Atanasov - web developer
E-mail me Send mail Subscribe Feed

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Authors

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012 it-coder.com

    Sign in