background preloader

Programming

Facebook Twitter

Java overloading vs overwriting. Why Favor Composition over Inheritance in Java and Object Oriented Programming. Favor composition over inheritance is a one of the popular object oriented design principle, which helps to create flexible and maintainable code in Java and other object oriented languages. Many times I have seen people suggesting use composition instead of inheritance, in fact my favorite books like Head first Design Patterns, also advocates this design principle. Head first books, has its own way of explaining, why composition is better than inheritance and though its long its quite interesting and informative. It was the first chapter of this book, which helped me a lot on understanding this key OOPS concept. In this Java and OOPS tutorial, I have tried to summarize my experience around composition and inheritance in two main category, first, difference between composition and inheritance and second, when to use Composition over inheritance in Java. 1) One reason of favoring Composition over Inheritance in Java is fact that Java doesn't support multiple inheritance. 5.

How to Generate Random Numbers in Java between Range - Example Tutorial. In software development and programming world we often needs to generate random numbers, some time random integers in a range e.g. 1 to 100 etc. Thankfully, Random number generation in Java is easy as Java API provides good support for random numbers via java.util.Random class, Math.random() utility method and recently ThreadLocalRandom class in Java 7, along with more popular features li ke String in Switch and ARM blocks . W hile random() method seems most convenient way of generating randoms in Java it only return random doubles, on the other hand by using Random, you can generate pseudo-random integer, floating point numbers e.g. double and even random boolean values. In this article Java tutorial, we will see how to generate random numbers in Java, examples to generating random integers and real numbers, and random numbers within a range e.g. between 1 to 6. we will also explore difference between Math.random() and java.util.Random class in Java.

Random Numbers in Java with Example. How I explained OOD to my wife. Introduction My wife Farhana wants to resume her career as a software developer (she started her career as a software developer, but couldn't proceed much because of our first child's birth), and these days, I am trying to help her learn Object Oriented Designs as I've got some experience in software design and development. Since my early days in software development, I have always observed that no matter how hard a technical issue seems, it always becomes easier if explained from a real life perspective and discussed in a conversational manner.

As we had some fruitful conversations on Object Oriented Designs, I thought I could share it because someone might find it an interesting way of learning OOD. Following is how our OOD conversation took place: Topic: Introducing OOD Shubho: Darling, let's start learning Object Oriented Designs. Farhana: You mean, Encapsulation, Inheritance, and Polymorphism, right? Shubho: OK, I hope you already know how to use classes and objects. Farhana: Yes. 10 Object Oriented Design Principles Java Programmer should know. The Abuse of Design Patterns in writing a Hello World Program | Taskinoor Hasan. “One comment I saw in a news group just after patterns started to become more popular was someone claiming that in a particular program they tried to use all 23 GoF patterns. They said they had failed, because they were only able to use 20.

They hoped the client would call them again to come back again so maybe they could squeeze in the other 3. Trying to use all the patterns is a bad thing, because you will end up with synthetic designs—speculative designs that have flexibility that no one needs. These days software is too complex. We can’t afford to speculate what else it should do. When people starts learning design patterns, they try to use patterns everywhere. Rather that calling “using patterns”, we can call this “abusing patterns”.

Let’s focus on an example. A beginner in programming will write a code like this (well, it’s in Java): This code seems too simple. First we define two interfaces Subject and Observer to add Observer. Then we add a Command. Then we add an Abstract Factory. Great Example of Open Closed Design Principle in Java. Great Example of Open Closed Design Principle I am big fan of design pattern article and love to read articles on design patterns and recently wrote about decorator design pattern in Java, Observer pattern, static factory pattern and about Singleton pattern. Today I come across this good article on open closed design pattern, what I like most is there example and clear way of explanation, first example is true value and it will help you understand open closed principle very quickly and second example is also not bad.

I have also shared many questions on design patterns and Object oriented design principles like 10 Java singleton interview questions , which discusses some of the most common questions on Singleton pattern like how to write Thread-Safe Singleton class , should we use Enum as Singleton or not etc and 20 design pattern interview questions, if you are preparing for senior level Java interview, where questions from design is expected, its worth looking for quick review.