Java OOPs Flashcards
What is OOPs?
OOPs (Object Oriented Programming) is a type of programming which is based on objects rather than just functions and procedures.
Individual objects are grouped into classes
What are the main features of OOPs?
- Inheritance
- Encapsulation
- Polymorphism
- Abstraction
What is an object?
An object is a unit of OOPs and is an instance of a class
What is a class?
A class is the blueprint for an object in Java, it defines the objects behavior/state
What is Inheritance?
Inheritance is the process in which the Child class acquires the properties of a Parent class using the ‘extends’ keyword
Advantage: Code reusability
Types of Inheritance
Single Inheritance: One Child class extends one Parent class
Multilevel Inheritance: Class A extends Class B and Class B extends Class C
Hierarchical Inheritance: Many child classes extends one Parent class
Hybrid Inheritance: Combination of Single and Multilevel Inheritance
Why is multiple inheritance not supported in Java?
If one Child class extends 2 Parent classes and both parent classes contain the same method, then the Child class would run into problems when it inherits them
What is Encapsulation?
Encapsulation is the process of binding data (Variables) and code (methods) into a single unit
Advantage: Security
Ex: Java Class (methods + variables) = capsule
How do you achieve Encapsulation?
Encapsulation can be achieved by:
- Creating PRIVATE variables in a class
- Providing Getter and Setter methods to make variables only accessible through those methods
What is Abstraction?
Abstraction is the process of hiding the implementation and providing only the necessary data at high level to the users
Advantage: Security
Ex: ATM machine (only the service is visible, not the implementation)
How do you achieve Abstraction in Java?
Abstract Class (Partial Abstraction)
Interface (Pure Abstraction)
What is an Abstract Class?
A class is made abstract when only partial implementation of the class is known
It can have abstract and non abstract methods, constructors and static methods
It can not be instantiated
What is an Interface?
It is a completely abstract class that is used to group related methods with empty bodies. uses the “implements” keyword in a class, which must give the empty methods their bodies
What are the differences between Abstract Class and Interface?
Abstract Class
- Abstract classes can have abstract and non abstract methods
- Does not support multiple Inheritance
- Uses the keyword ‘extends’
- Ex: public abstract class Shape{public abstract void draw();};
Inheritance
- Inheritance can only have abstract methods
- Supports multiple Inheritance
- Uses the keyword ‘implements’
- Ex: public interface Drawable{void draw();};
What is Polymorphism?
Polymorphism is the process in which one object is used in many forms
It occurs when we have classes that are related to each other by inheritance
What are the types of Polymorphism?
Static / Compile Time Polymorphism - Method Overloading
Dynamic / Run Time Polymorphism - Method Overriding
What is Method Overloading?
Same method name, but different method arguments
Advantage: Makes code simpler
Ex: void add(int a, int b); void add(int a, int b, int c);
What is Method Overriding?
Same method, same arguments. It is used in Parent-Child relationship/inheritance classes
Advantage: Code reusability
Can we overload a static method in Java?
Yes, you can overload a static method in Java as they are with different method signatures, this no problem will occur
Can we override a static method in Java?
No, you cannot override a static method because static methods belong to a class and resolved at compile time using the type of reference variable
Can we override a private method in Java?
No, since the private method is only accessible and visible inside the class they are declared
What are the rules for method overriding in Java?
If the Parent class throws checked exceptions, then the Child class should throw that exception or its subtype exception
But no restriction for unchecked exceptions as both classes are independent
Is Java 100% Object Oriented?
No, because primitive data types are not considered objects, the same with static classes, as they can be used without an object
What is the difference between String, StringBuilder, and StringBuffer?
- A string is an object representation of char values which is immutable, internally, strings are a special type of char array
- StringBuilder and StringBuffer both create mutable strings objects, but the difference between the two is that StringBuffer is thread safe, whereas StringBuilder is not