Skip to content

Tag: oop

Comparing two classes by its types or class names

There is need to compare two objects based on class they implement? When to compare using getClass() and when getClass().getName()? Is there any difference between this approaches to compare two Objects class types (names)? EDIT 1 What about: ? Answer Is there any difference between this approaches to compare…

Why can attributes in Java be public?

As everybody knows, Java follows the paradigms of object orientation, where data encapsulation says, that fields (attributes) of an object should be hidden for the outer world and only accessed via methods or that methods are the only interface of the class for the outer world. So why is it possible to declar…

How to do method overloading for null argument?

I have added three methods with parameters: When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char[] methods or Integer and Object methods? Answer Java will always try to use the most specific applicable version of a method that’s …

The purpose of interfaces continued

OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract. And I semi see the point of them. But if all you have in the interface is: and it has no implementation as such, then whoever uses your i…

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder? Answer Mutability Difference: String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Differenc…