Java OOPS concept questions
Multiple Inheritance
*Not Allowed in Java*. A child class inherits the property from multiple classes. If multiple parent classes has the same name as a method, then at runtime it becomes difficult for compiler to decide which method to execute from the child class
What is the difference between Polymorphism and Inheritance
- Inheritance Defines parent-Child relationship between two classes. polymorphism takes advantage of that relationship to add dynamic behavior in your code. - Inheritance helps with code reusability. polymorphism allows Child to redefine what's already defined inside the parent class.
Abstraction
A way of converting real world objects in terms of class. Its a concept of defining an idea in terms of classes or interface. Call the objects in the class outside of the method.
Encapsulation
Achieved by combining the methods and attributes into a class. The class acts like a container, encapsulating the properties. the user are exposed to mainly public methods. The idea behind it is to hide how things work and just exposing the requests a user can do
Overloading
Determined at the compile time. Occurs when several methods have the same name with: - different method signature and different number or type parameters
Inheritance
Inheritance allows a child class to inherit properties from its parent class. This is achieved using extends keyword in Java. Only properties with access modifier public and protected can be accessed in the child class
We want a class to be:
Runnable, Comparable, and serializable
Composition
Specialized form of aggregation and we can call this as a "death" relationship. A strong type of aggregation. Child objects don't have their own life cycle and if the parent object is deleted, so is the child.
Aggregation
Specialized form of association where all objects have their own lifecycle but there is ownership and child objects cannot belong to another parent object. EX: A teacher is in only one department but if the department object is destroyed then the teacher is still there
What is polymorphism?
The ability to identify a function to run is called polymorphism. There are two kinds of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding)
Method Overriding
When a class method has the same name and signature as a method in the parent class. The correct method to call is determined at compile time
Association
a relationship where all objects have their own lifecycle and there is no owner. Ex: Teachers have multiple students and students have multiple teachers. There is no ownership b/t the objects and they both have their own lifecycle. Both can create and delete independently.