What is the difference between an abstract class and an interface and when should you use them?

In design, you want the base class to present only an interface for its derived classes. This means, you don’t want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behavior), so that its interface can… Continue reading

Explain the assertion construct?

The assertion statements have two forms as shown below: assert Expression1; assert Expression1 : Expression2; Where: Expression1 -> is a boolean expression. If the Expression1 evaluates to false, it throws an AssertionError without any detailed message. Expression2 -> if the Expression1 evaluates to false throws an AssertionError with using the… Continue reading

What is design by contract? Explain the assertion construct?

Design by contract specifies the obligations of a calling-method and called-method to each other. Design by contract is a valuable technique, which should be used to build well-defined interfaces. The strength of this programming methodology is that it gets the programmer to think clearly about what a function does, what… Continue reading

Why would you prefer code reuse via composition over inheritance?

Both the approaches make use of polymorphism and gives code reuse (in different ways) to achieve the same results but: The advantage of class inheritance is that it is done statically at compile-time and is easy to use. The disadvantage of class inheritance is that because it is static, implementation… Continue reading

Which one to use Implementation inheritance OR Interface inheritance with composition in JAVA?

Prefer interface inheritance to implementation inheritance because it promotes the design concept of coding to an interface and reduces coupling. Interface inheritance can achieve code reuse with the help of object composition. If you look at Gang of Four (GoF) design patterns, you can see that it favors interface inheritance… Continue reading

What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding?

Polymorphism – means the ability of a single variable of a given type to be used to reference objects of different types, and automatically call the method that is specific to the type of object the variable references. In a nutshell, polymorphism is a bottom-up method call. The benefit of… Continue reading

Which one to favor, composition or inheritance?

The guide is that inheritance should be only used when subclass ‘is a’ superclass. Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if… Continue reading

How do you express an ‘is a’ relationship and a ‘has a’ relationship or explain inheritance and composition? What is the difference between composition and aggregation?

The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition. Inheritance is uni-directional. For example House is… Continue reading