33 Responses

  1. chaitanya
    chaitanya February 11, 2013 at 3:59 am | | Reply

    EXCELLENT WORK OUT HARSHA… 🙂
    VERY USEFUL EXAMPLE GIVEN IN IS-A EXPLANATION… 🙂

  2. Day1 – OOPS
    Day1 – OOPS March 19, 2013 at 5:03 am |

    […] IS-A, HAS-A Relationship […]

  3. deepakgrt
    deepakgrt September 26, 2013 at 2:12 pm | | Reply

    ur 3rd point is wrong sir.
    if you are saying inheritance is for reusability.

    1. yaswanthYeswanth
      yaswanthYeswanth December 19, 2013 at 7:33 am | | Reply

      It is correct deepak, Inheritance can be used for re-usability

    2. Wajid Ali
      Wajid Ali June 21, 2014 at 2:04 am | | Reply

      It is used for Reusibility

  4. usha
    usha December 19, 2013 at 1:01 am | | Reply

    Good Explanation sir..!

  5. Wajid Ali
    Wajid Ali June 21, 2014 at 2:03 am | | Reply

    Very nice work Harsha! It was fantastic……..!

  6. shaik alla basha
    shaik alla basha September 4, 2014 at 1:48 pm | | Reply

    gooooooooood sir………….

  7. krishna singh
    krishna singh September 13, 2014 at 7:21 am | | Reply

    Please check this
    P p = new C();

    p.m1(); // compiles fine

    p.m2(); // Will get a compile time error as “Cannot find m2()”

    Just give a try p.m2 will work

  8. yogesh
    yogesh September 21, 2014 at 9:05 am | | Reply

    diff. between class and interface…???

  9. yogesh
    yogesh September 21, 2014 at 9:06 am | | Reply

    what is the use polymorphism in programme..??

  10. venkat
    venkat October 9, 2014 at 6:57 am | | Reply

    actually
    P p = new C();

    p.m1(); // compiles fine

    p.m2(); // It is working compiles fine

  11. Prasanta
    Prasanta October 20, 2014 at 2:57 am | | Reply

    article is very nice. thank you.

  12. Amit
    Amit October 26, 2014 at 8:09 am | | Reply

    thnx for understanding is-a relation ship

  13. kun
    kun November 6, 2014 at 5:15 am | | Reply

    thanx for help

  14. Mangesh
    Mangesh November 12, 2014 at 1:44 am | | Reply

    Sir is correct. I tried to compile following code.

    package com.test.corejava;

    class P

    {

    public void m1() {}

    }

    class C extends P

    {

    public void m2() {}

    }

    class Test

    {

    public static void main (String [] args)

    {

    //case 1

    P p = new P();

    p.m1(); // compiles fine

    p.m2(); // Will get a compile time error as “Cannot find m2()”

    //case 2

    C c = new C();

    c.m1(); // compiles fine

    c.m2(); // compiles fine

    //case 3

    P p2 = new C();

    p2.m1(); // compiles fine

    p2.m2(); // Will get a compile time error as “Cannot find m2()”

    //case 4

    C c1 = new P(); // Compiler error as “incompatible types”

    }

    }

  15. Dhiraj Singh
    Dhiraj Singh November 24, 2014 at 11:02 pm | | Reply

    Yes, the code

    p2.m2(); //compilation error

    will give compilation error.

    Reason is: Using super class reference we can call only those methods whose signature is available in both sub class as well as super class. Here Super class P does not have signature of m2().

  16. Cindy
    Cindy February 1, 2015 at 11:50 am | | Reply

    I have a doubt. In the following code, is the statement A HAS-A B true even though we have not instantiated B?

    class B{ }
    public class A{
    B b;
    }

    1. VINURA
      VINURA July 2, 2015 at 6:06 am | | Reply

      It become true ONLY if we instantiated B
      class B{ }
      public class A{
      B b=new B();
      }

  17. priya
    priya February 17, 2015 at 11:35 pm | | Reply

    can any one tell what is the relationship exist between sevicecentres and vehicles

  18. Naresh
    Naresh June 15, 2015 at 12:49 am | | Reply

    Harsh Its great work and very helful for us

  19. Ramakrishna
    Ramakrishna July 14, 2015 at 11:36 pm | | Reply

    its very useful to known what is is-a relation and what is has-a relation and Thanks you Harsh……….

  20. Raveesh Sharma
    Raveesh Sharma July 20, 2015 at 5:09 am | | Reply

    Hi .. Great Article. Might I add a few pointers:
    Although code re usability maybe a reason for inheritance, I think the prime need for interfaces / abstract classes is generalization. Even when you look through the java code all interfaces that you see are generalized operations that should be supported by child classes. SO i think its fair to say that Is- a relationship is tending more towards generalization.

    Regarding Composition and aggregation. The explanation is very correct. Just wanted to add the java code for the same.

    //Composition relationship
    class University{
    private Department departments;

    University(){
    //Creating a university object depends on creating department objects.
    //This indicates university is composed of depratments
    departments = new Department()

    }
    }

    //Aggregation relationship
    class Department{
    //Department consists of persons
    private Person p;

    //Methods which use the person Obj.
    }

  21. name
    name August 11, 2015 at 10:05 am | | Reply

    all your examples are straightaway from Durga sir classes

  22. nish
    nish November 1, 2015 at 11:31 am | | Reply

    simple and cute defination.,.. thanks

  23. Ilyas Mohamed
    Ilyas Mohamed July 15, 2016 at 11:24 am | | Reply

    Thank you, its useful

  24. Anees Farhana
    Anees Farhana January 10, 2017 at 9:53 am | | Reply

    what is container object and contained objecf sir?

  25. Bharath
    Bharath May 21, 2017 at 9:18 am | | Reply

    Thanks a million, your example cleared my confusion forever !!!!!

  26. Sandesh
    Sandesh July 19, 2017 at 1:15 pm | | Reply

    If you want to use that m2() method using parent reference then you need to implement downcasting.

    Code is:

    C c = new C();

    P ref;

    ref=c;
    (C)ref.m2(); //Compiles fine without any error

  27. Manish
    Manish September 20, 2017 at 1:30 am | | Reply

    Can u tell me the which relation (IS-A or HAS-A)is good or recommended to use…
    why. it is recommended to use…? explain me

  28. sreelakshmi
    sreelakshmi October 9, 2017 at 5:54 am | | Reply

    sir understood above code which u explained….
    but please give me brief explanation about instanceOF key word.. which is used to identify is a relation

  29. abhay pratap singh
    abhay pratap singh November 21, 2017 at 12:09 pm | | Reply

    Very good explanation

Leave a Reply to kun Cancel reply