HI WELCOME TO SIRIS
Showing posts with label oops. Show all posts
Showing posts with label oops. Show all posts

Difference Between Generalization and Specialization

The process of extracting common characteristics from two or more classes and combining them into a generalized superclass, is called Generalization. The common characteristics can be attributes or methods. Generalization is represented by a triangle followed by a line.
Specialization is the reverse process of Generalization means creating new sub classes from an existing class.
Let’s take an example of Bank Account; A Bank Account is of two types – Current Account and Saving Account. Current Account and Saving Account inherits the common/ generalized properties like Account Number, Account Balance etc. from a Bank Account and also have their own specialized properties like interest rate etc.


What do you think?
I hope you will enjoy the tips while programming with OOPs. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding Inheritance and Different Types of Inheritance

Inheritance is a mechanism of acquiring the features and behaviours of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.
For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.

Different Types of Inheritance

OOPs support the six different types of inheritance as given below :
  1. Single inheritance
  2. Multi-level inheritance
  3. Multiple inheritance
  4. Multipath inheritance
  5. Hierarchical Inheritance
  6. Hybrid Inheritance

  1. Single inheritance

    In this inheritance, a derived class is created from a single base class.
    In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.


    Syntax for Single Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
  2. Multi-level inheritance

    In this inheritance, a derived class is created from another derived class.
    In the given example, class c inherits the properties and behavior of class B and class B inherits the properties and behavior of class B. So, here A is the parent class of B and class B is the parent class of C. So, here class C implicitly inherits the properties and behavior of class A along with Class B i.e there is a multilevel of inheritance.


    Syntax for Multi-level Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : B
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
  3. Multiple inheritance

    In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc. and Java Language.
    In the given example, class c inherits the properties and behavior of class B and class A at same level. So, here A and Class B both are the parent classes for Class C.


    Syntax for Multiple Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Base Class
    11. class B
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : A, B
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
  4. Multipath inheritance

    In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.
    In the given example, class D inherits the properties and behavior of class C and class B as well as Class A. Both class C and class B inherits the Class A. So, Class A is the parent for Class B and Class C as well as Class D. So it's making it Multipath inheritance.



    Syntax for Multipath Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : A
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
    27.  
    28. //Derived Class
    29. class D : B, A, C
    30. {
    31. public void fooD()
    32. {
    33. //TO DO:
    34. }
    35. }
  5. Hierarchical Inheritance

    In this inheritance, more than one derived classes are created from a single base class and futher child classes act as parent classes for more than one child classes.
    In the given example, class A has two childs class B and class D. Further, class B and class C both are having two childs - class D and E; class F and G respectively.


    Syntax for Hierarchical Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : A
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
    27.  
    28. //Derived Class
    29. class D : C
    30. {
    31. public void fooD()
    32. {
    33. //TO DO:
    34. }
    35. }
    36.  
    37. //Derived Class
    38. class E : C
    39. {
    40. public void fooE()
    41. {
    42. //TO DO:
    43. }
    44. }
    45.  
    46. //Derived Class
    47. class F : B
    48. {
    49. public void fooF()
    50. {
    51. //TO DO:
    52. }
    53. }
    54.  
    55. //Derived Class
    56. class G :B
    57. {
    58. public void fooG()
    59. {
    60. //TO DO:
    61. }
    62. }
  6. Hybrid inheritance

    This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.
    Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritance is not supported by .NET Languages.



    Syntax for Hybrid Inheritance

    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Base Class
    11. class F
    12. {
    13. public void fooF()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class B : A, F
    21. {
    22. public void fooB()
    23. {
    24. //TO DO:
    25. }
    26. }
    27.  
    28. //Derived Class
    29. class C : A
    30. {
    31. public void fooC()
    32. {
    33. //TO DO:
    34. }
    35. }
    36.  
    37. //Derived Class
    38. class D : C
    39. {
    40. public void fooD()
    41. {
    42. //TO DO:
    43. }
    44. }
    45.  
    46. //Derived Class
    47. class E : C
    48. {
    49. public void fooE()
    50. {
    51. //TO DO:
    52. }
    53. }

Advantages of Inheritance

  1. Reduce code redundancy.
  2. Provides code reusability.
  3. Reduces source code size and improves code readability.
  4. Code is easy to manage and divided into parent and child classes.
  5. Supports code extensibility by overriding the base class functionality within child classes.

Disadvantages of Inheritance

  1. In Inheritance base class and child class, both are tightly coupled. Hence If you change the code of parent class, it will affect all the child classes.
  2. In class hierarchy, many data members remain unused and the memory allocated to them is not utilized. Hence it effect performance of your program, if you have not implemented inheritance correctly.
What do you think?
In this article, you learned about the different types of inheritance including advantages and disadvantages. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding Association, Aggregation, Composition and Dependency relationship

A relationship defines the connection between objects. This explains how objects are connected to each other’s and how they will behave.

Association

It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of relationship between objects. This is represented by a solid line.
Let’s take an example of relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

Aggregation

It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.
Let’s take an example of relationship between Department and Teacher. A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.


Composition

It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.
Let’s take an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete.

Let’s take another example of relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will be automatically deleted.

Dependency

It represents a relationship between two or more objects where an object is dependent on another object(s) for its specification or implementation. This is represented by a dashed arrow.
Let’s take an example of relationship between client and service. A client is dependent on the service for implementing its functionalities.


Let’s take another example of relationship between a client and a supplier. A client is dependent on the supplier for supplying products. If the supplier will not supply the products, client cannot use those products.
What do you think?
I hope you will enjoy the tips. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Difference between function and method

In programming langauages we have two concepts functions and methods. functions are defined in structural language and methods are defined in object oriented langauge. The difference between both is given below :

Functions

  1. Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
  2. Functions are defined in structured languages like Pascal,C and object based language like javaScript
  3. Functions are called independently.
  4. Functions are self describing unit of code.
  1. //function main in C
  2. void main()
  3. {
  4. int a,b,c;
  5. a=5;
  6. b=6;
  7. c=a+b;
  8. printf("Sum is : %d",c);
  9. }

Methods

  1. Methods do not have independent existence they are always defined with in class. Ex:- main() method in C# Language that is defined with in a class
  2. Methods are defined in object oriented languages like C#, Java
  3. Methods are called using instance or object.
  4. Methods are used to manipuate instance variable of a class.
  1. //method sum in C#
  2. class demo
  3. {
  4. int a,b,c;
  5. public void sum()
  6. {
  7. a=5;
  8. b=6;
  9. c=a+b;
  10. Console.WriteLine("Sum is : {0}",c);
  11. }
  12. }

Difference between object oriented and object based languages

Object oriented and Object based programming languages have some different features and behaviour. In this article, I am going to expose the main difference between these two programming languages.

Object oriented language

  1. Object-oriented language supports all the features of OOPs.
  2. Object-oriented language doesn't has in-built object.
  3. Object-oriented languages are C++, C#, Java etc.

Object based language

  1. Object-based language doesn't support all the features of OOPs like Polymorphism and Inheritance
  2. Object-based language has in-built object like javascript has window object.
  3. Object-based languages are Javascript, VB etc.
Summary
In this article I try to expose the difference between object oriented and object based languages . I hope after reading this article you will be able to diffrentiate between these two languages. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.