click below
click below
Normal Size Small Size show me how
JAVA OOP
Interview prep
Question | Answer |
---|---|
OOP Object Oriented Programming | programming paradigm building blocks: Class and Object. |
Class | Attributes and methods defining an Object |
Object | Instance of a class with it's particular methods and properties |
OOP Concepts | Polymorphism Inheritance Abstraction Encapsulation +Aggreagation +Composition +Association |
Polymorphism | the ability of an object to take on many forms two types: compile time (overloading) and runtime (overriding). |
Inheritance | allows a Child class to inherit properties from its parent class extends keyword Only properties with access modifier public and protected can be accessed in child class. |
Abstraction | a way of converting real world objects in terms of class. |
Encapsulation | Combination of methods and attribute into a class. The class protect it's properties by exposing only public methods. |
Aggreagation | child object can not belongs to another parent object. 1 teacher -> 1 dept if we delete the department teacher object will not destroy |
Composition | if parent object deletes all child object will also be deleted 1 house -> N rooms |
Association | relationship where all object have their own lifecycle and there is no owner. Both can create and delete independently. Ex: N students -> 1 teacher 1 student -> N teachers |
Overloading | compile time polymorphism same name but different method signature or different number or type of parameters. |
Overriding | runtime polymorphism method in a child class has the same name and signature as a method in the parent class. |
multiple inheritance | If a child class inherits the property from multiple classes |
does java support multiple inheritance? | No. at runtime it becomes difficult for the compiler to decide which method to execute from the child class. |
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. |
does java support multiple inheritance of interface? | Yes. Resolves the Diamond problem. |
Diamond problem. | ambiguity where the compiler doesn't know which superclass method to execute |
Interface | implement keyword can only contain public abstract methods. variable is by default public final contract: its mandatory to implement all of its methods |
abstract class | extends keyword mandatory to implement all abstract methods. |
access modifiers | Private: inside the class Protected: same package, or diff pkg if the class extends the main class Default: only accessible in the same package Public: accessible from a diff pkg |
final keyword | Class : cannot be extended. method: cannot be overridden. variable: value cannot be changed. Objects: instantiated only once |
Synchronized keyword | Only one thread at a time can access synchronized methods |
Static variable | Multiples objects of a class shares the same instance of a static variable. |
Static method | can be accessed without creating the objects can only access static variables and not local or global non-static variables |
Static class | if all the variables and methods of the class are static and the constructor is private. prevent the class to be instantiated. So the only possibility to access is using Class name only |
Throw keyword | used to throw the exception manually subclass of Throwable. |
JDK, JRE and JVM | Java Development Kit: software development tools Java Runtime Environment: Library and classes, env to execute Java applications Java Virtual Machine: Runtime Instance |