Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Substitution principle ( is a vs is like a relationship )

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Substitution principle ( is a vs is like a relationship )

    Hey guys
    I still pretty new to Java and how it works.
    I am think I am getting the hang of how Inheritance is working
    except this idea of the substitution principle and the is like a relationship
    Can anybody explain this preferably with some code so I can understand !

    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Substitution principle ( is a vs is like a relationship )

    The substitution principle means just that: you can substitute in a class with another one which inherits from that class. When you inherit a class you should be able to do a drop-in replacement without any other changes. (Basically a restatement of the Liskov substitution principle). Anything which violates this is considered a "is like a" relationship.

    This Wikipedia article provides an interesting example:

    A square is like a rectangle. They both have 4 sides and 4 right angles. It may even make sense for a square class to extend a rectangle class since all squares mathematically are rectangles. However, this violates the the Liskov substitution principle because a square must have all sides of the same length while a rectangle has a width and height. It's reasonable to assume a rectangle class would have a setWidth() and setHeight() methods which can set each field separately. However, these methods don't make much sense for squares. You might alter the setWidth() and setHeight() methods to set the side length instead, but this alters the fundamental principle of how these methods are suppose to work: they set the width and height differently. So this would break code that looks like this:

    Rectangle a = new Square();
    a.setWidth(5);
    a.setHeight(3);
    System.out.println("Area of a is " + a.getArea()); // should be 15, but here it possibly 9 or undetermined

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Substitution principle ( is a vs is like a relationship )

    Hey sorry for the late reply I think I get it now thanks

Similar Threads

  1. How would you design the relationship between these two classes?
    By jrahhali in forum Object Oriented Programming
    Replies: 5
    Last Post: March 10th, 2012, 09:50 PM
  2. Relationship of sizes
    By zscipio in forum Java SE APIs
    Replies: 1
    Last Post: January 31st, 2012, 01:18 PM
  3. Principle of Truth in Advertising
    By Richardinho in forum Collections and Generics
    Replies: 1
    Last Post: April 12th, 2011, 03:52 PM
  4. type substitution - whats the theory behind it
    By mds1256 in forum Java Theory & Questions
    Replies: 0
    Last Post: January 20th, 2010, 07:40 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM