More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Composition - has-a relationship between objects.
Inheritance - is-a relationship between classes.
Composition - Composing object holds a reference to composed objects and hence relationship is loosely bound.
Inheritance - Derived object carries the base class definition in itself and hence its tightly bound.
Composition - Used in Dependency Injection
Inheritance - Used in Runtime Polymorphism
Composition - Single class objects can be composed within multiple classes.
Inheritance - Single class can only inherit 1 Class.
Composition - Its the relationship between objects.
Inheritance - Its the relationship between classes.
Ans. 1.Abstraction solves the problem at design level while encapsulation solves the problem at implementation level
2.Abstraction is used for hiding the unwanted data and giving relevant data. while Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something.
4.For example: Outer Look of a Television, like it has a display screen and channel buttons to change channel it explains Abstraction but Inner Implementation detail of a Television how CRT and Display Screen are connect with each other using different circuits , it explains Encapsulation.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Overriding method can not be more restrictive than the overridden method.
reason : in case of polymorphism , at object creation jvm look for actual runtime object. jvm does not look for reference type and while calling methods it look for overridden method.
If by means subclass were allowed to change the access modifier on the overriding method, then suddenly at runtime when the JVM invokes the true objects version of the method rather than the reference types version then it will be problematic
2. In case of subclass and superclass define in different package, we can override only those method which have public or protected access.
3. We can not override any private method because private methods can not be inherited and if method can not be inherited then method can not be overridden.
Ans. Car Engine is an example of encapsulation and abstraction. You ignite the car using an interface called starter and least bothered about how the tire actually moves (This is abstraction). The engine encapsulates the complete process to itself only and doesn't allow you to start the other components like the radiator etc ( this is excapsulation )
Help us improve. Please let us know the company, where you were asked this question :
Ans. Derived object carries the body of its class as well as the body of the parent class. Its body ( member elements ) is initialized using its own class constructor whereas the body ( member elements ) carried from the parent class are initialized using super class constructor. So In order to initialize the elements of the parent class before its own elements are even initialized, super is called.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.
2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception
3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method
Help us improve. Please let us know the company, where you were asked this question :
Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method.
Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.
C++ compiles to machine language , when Java compiles to byte code .
In C++ the programmer needs to worry about freeing the allocated memory , where in Java the Garbage Collector takes care of the the unneeded / unused variables.
Java is platform independent language but c++ is depends upon operating system.
Java uses compiler and interpreter both and in c++ their is only compiler.
C++ supports operator overloading whereas Java doesn't.
Internet support is built-in Java but not in C++. However c++ has support for socket programming which can be used.
Java does not support header file, include library files just like C++ .Java use import to include different Classes and methods.
There is no goto statement in Java.
There is no scope resolution operator :: in Java. It has . using which we can qualify classes with the namespace they came from.
Java is pass by value whereas C++ is both pass by value and pass by reference.
Java Enums are objects instead of int values in C++
C++ programs runs as native executable machine code for the target and hence more near to hardware whereas Java program runs in a virtual machine.
C++ was designed mainly for systems programming, extending the C programming language whereas Java was created initially to support network computing.
C++ allows low-level addressing of data. You can manipulate machine addresses to look at anything you want. Java access is controlled.
C++ has several addressing operators . * & -> where Java has only one: the .
We can create our own package in Java(set of classes) but not in c and c++.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  java   c++   difference between java and c++   programming concepts   programming languages   architecture   technical architect   technical lead Asked in 1 Companies
Q10. Why Java is not considered pure OOP's language ?
1. Usage of Primitive types - Though Java provides classes for the primitive data types but as the usage of primitives is permissible, its considered unpure OOP's language.
2. Usage of Static members - Static members belong to the class and not objects and hence not considered fit for pure OOP's programming.
Help us improve. Please let us know the company, where you were asked this question :
Ans. A source of possible errors in parallel programming, where one thread can cause an error in another thread by changing some aspect of the state of the program that the second thread is depending on (such as the value of variable).
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.
We cannot access methods defined in String class or in any class in hierarchy between String and Object.
For example - we cannot do obj.append("abc") as it will now give compile time error.
Help us improve. Please let us know the company, where you were asked this question :
Q17. How does java identifies which method to be called in method overriding or runtime polymorphism, when both methods share the same name and signature ?
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Enforcing composition over inheritance
2. Restricting overriding of certain methods
3. Final methods are faster than regular instance methods
4. Enforcing Immutability
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  final class   reasons for final class   restricting inheritance  object oriented programming (oops)  oops concepts expert
Ans. In GUI programming, an object that can be registered to be notified when events of some given type occur. The object is said to listener? for the events.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
Help us improve. Please let us know the company, where you were asked this question :
Q23. What are the advantages and Disadvantages of Sockets ?
Ans. Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications. It cause low network traffic.
Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Stateless objects are the objects without instance fields (instance variables). The class may have compile time constants i.e static final fields.Immutable objects are the objects which have state but the state cannot be changed after initialization. Both are Thread safe.
Help us improve. Please let us know the company, where you were asked this question :
This is a very sensitive question and should be dealt with caution. Just simply saying that you never had any disagreement will present you as dumb team member. Showing your self as too aggressive in such decisions will present you as a trouble maker. You should present a situation where you had an argument / disagreement but eventually you and your team mates mutually found a way out of it.
Help us improve. Please let us know the company, where you were asked this question :