advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Liskov's Substitution Principle :

Liskov's Substitution Principle

by Samudra Gupta

That Java is an Object Oriented language does not necessarily mean that the code written in Java is always Object Oriented. If this statement surprises you, this series is for you. In this series, I will try to demonstrate some design aspects, both good and bad, that are the key to well written software in Java. The first of these is the Liskov’s Substitution Principle (LSP), which will lead to the Design by Contract and Dependency Inversion Principle. We will then see how all of them conform to one most vital principle of OO design, the Open-Closed Principle.

Liskov’s Substitution Principle

The Liskov’s Substitution Principle provides a guideline to sub-typing any existing type. Stated formally it reads: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behaviour of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

In a simpler term, if a program module is using the reference of a Base class, then it should be able to replace the Base class with a Derived class without affecting the functioning of the program module.

An example

When I was in the beginning of my IT career, I was working on a banking project and I was asked to design an account-handling module. To simplify the scenario, let us assume that I had two types of accounts to handle. One is the "Current Account" and the other is a special type of current account with a better interest rate, but with some restrictions that the account cannot be closed before a certain time period. Having done the preliminary analysis, I decided to come up with two account objects "CurrentAccount" and "SpecialCurrentAccount," and also decided to write another class that would offer the interfaces to operate on these "XXXAccount" objects. I also analysed that the "SpecialCurrentAccount" and "CurrentAccount" share lot in common. Delighted with such a straightforward relationship, I came up with the following class diagram. The SpecialCurrentAccount is a sub-type of the CurrentAccount.

The program specification for the module specified the following constraints.

  • The closing of Current Account should check for a balance greater than zero. If satisfied, proceed to close the account.
  • The closing of Special Current Account should check for a balance greater than zero and also that the minimum period for the account is covered. If satisfied, proceed to close the account.

From the above specification and my own class diagram, I decided that I will override a closeAccount() method in the derived SpecialAccount class. The following programs CurrentAccount.java (Listing 1) and SpecialCurrentAccount.java (Listing 2) describe the designed classes.

/*
 * Account.java
 *
 */

package sam.oo.bad;

public class CurrentAccount {
    
    /** Holds value of property balance. */
    protected int balance;
    
    /** Holds value of property period. */
    protected int period;
    
    /** Creates a new instance of Account */
    public CurrentAccount(int balance, int period) {
        this.balance = balance;
        this.period = period;
    }
    
    /**
     * open a current account with the given balance
     */
    public boolean openAccount(int balance)
    {
        this.balance = balance;
        return true;
    }
    
    /**
     *closes the account
     */
    public boolean closeAccount()
    {
        if(balance >0)
            return true;
        else
            return false;
    }
    
    /** Getter for property balance.
     * @return Value of property balance.
     */
    public int getBalance() {
        return this.balance;
    }
    
    /** Setter for property balance.
     * @param balance New value of property balance.
     */
    public void setBalance(int balance) {
        this.balance = balance;
    }
    
    /** Getter for property period.
     * @return Value of property period.
     */
    public int getPeriod() {
        return this.period;
    }
    
    /** Setter for property period.
     * @param period New value of property period.
     */
    public void setPeriod(int period) {
        this.period = period;
    }
    
}
Listing 1 CurrentAccount.java
 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs