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