Skip to main content


Showing posts from September, 2007

Inheritance and composition

Inheritance is used for Code resuse To support polymorphism Disadvantages of inheritance Difficult to change the interaface of the subclass Superclass is always instantiated Can get complex and unmanageable Composition is used for code reuse Disadvantages of composition We cannot use polymorphism We cannot extend the system by adding subclasses May have a slight performance overhead Usage Inheritance: IS- A Composition: HAS - A Example 1 : public class Car {    private Engine engine;    public void start () {}    public void stop () {} } public class SUV extends Car {    public void start () {} //overrides the start method from Car    public void fourByFourMode () {} } Example 2 : public void someMethod () {    Car c = new SUV () ;    c.start () ; } Example 3 : public class Car {    private Engine engine;    public void start () {      engine.start () ;      //maybe do something more    }    public void stop () {} }

High cohesion

[Time: 4 mins] Stuff that goes together, stays together Easy to discover relevant code Levels of cohesion packages classes methods So in very simple words, cohesion is keeping related stuff together so we can find what we want without too much difficulty. Example 1 : public class Student { //cohesive methods public void registerForCourse () {} public void deregisterFromCourse () {} viewTranscripts () {} //methods below are not in synch with the responsibilities of the Student class //hence cohesion is broken submit grades () {} submitCourseSchedule () {} } Java2html Resources: High cohesion on Wikipedia Note: This text was originally posted on my earlier blog at

Loose coupling

[Time: 4:32 mins] Loose (or low) coupling means (in a generic sense) that an entity can interact with another entity without being concerned with the internal implementation, and through a stable interface. Low coupling is implemented by using principles of information hiding. When two entities are loosely coupled, internal changes in one of them do not affect the other. The calling method is coupled to the called method and not it's implementation. Since we have already discussed encapsulation and information hiding in detail, I will not repeat it here. Loose coupling is achieved with information hiding And by programming to the interface rather than the implementation Example 1 : Tightly coupled system public void aMethod () { ArrayList myList = new ArrayList () ; doSomethingWithList ( list ) ; } private void doSomethingWithList ( ArrayList list ) { } Example 2 : Loosely coupled system public void aMethod () { List myL ist

Don't repeat yourself

The code says everything once and only once, which is the essence of good design. -- Martin Fowler [Time: 4:44 mins] Repititive code makes maintainance more difficult There are more places to change We may forget to change some of them Increases probability of bugs Types of repitition In methods in a class Accross the entire software Example 1: DRY violated public class Account { public void transferFunds ( double amt, Account dest ) { //transfer funds //generate email... create connection //create message //dispatch email } public void delete () { //delete account //generate email... create connection //create message //dispatch email } } Java2html Example 2: DRY honoured public class Account { public void transferFunds ( double amt, Account dest ) { //transfer funds dispatchEmail ( ... ) ; } public void delet

Keep it simple

[Time: 5:37 mins] One should not increase, beyond what is necassary, the number of entities required to explain anything - Occams Razor KISS helps keep software manageable The UNIX kernel is an example of a functionally complex software implemented in simple design Intentional complexity Enthusiasm to use design patterns Enthusiasm to make a system uber flexible Feature bloat Unintentional complexity Maintainance quick fixes Laziness in refactoring Resources: PrematureGeneralization Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at

Software design principles

Over the next few posts, I will cover some basic software design principles. There are some well known software design principles that serve as guideposts for us in the design process. But here is something to always keep in mind while using design principles to guide us in our design. While principles, patterns, heuristics, and guidelines offer valuable advice about how to do things the right way, they don't serve as substitution for thinking on our own. Unfortunately, I think we all too often let these principles, etc. do the thinking for us. -- Kirk Knoernschild The audiio snippet below is a very simple and brief introduction to software design. Please click on the start arrow to begin the audio. Stay tuned for more on software design in the next few posts posts... [Time: 2:20 mins] Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at

A student's perspective on Java

From some time I have been very keen to publish a post that describes a student's perspective on Java. Over the years Java has gone from a simple easy to learn language to quite a huge beast. The volume of things one has to learn and keep up in Java poses a special challenge to students who wih to learn the language. I would like to post experiences of how various students coped with the challenge of learning Java. In this post, Sanket Daru, a student in one of my Java classes at SCIT describes his experience with Java. I am particularly glad to post Sanket's thoughts, since he is one of the brightest and most enthusiastic students, that I have had the pleasure of teaching. Q. How did you go about learning Java? A. It all began in 2003 during my graduation. We had Java in our curriculum. I attended the Java faculty’s first and second lecture and knew that “maybe he is a genius, but he knows nothing about the heads or tails of teaching.” Convinced and determine

Compiling Java source files with supplementary characters

Java source files can also contain supplementary characters as strings as well as identifiers if the character is a string or a digit. Here is a video that shows how we can compile Java source files that contain supplementary characters as Strings. Click on the image to download the video.     Note: This text was originally posted on my earlier blog at