OOPs Flashcards
Entity
an entity is a single person, place, or thing about which data can be stored.
Characterictics of object
An object has three charactertics:
- State : value of object
- Behaviour : functionality of the object
- Identity : Unique ID. For JVM to identify each object uniquely. (Values of the id is not visible to the external user)
For example:- Pen is an object. Its name is Reynolds; color is white, known as its state. It is used to write, so writing is its behaviour
Object
Any one definition would do…
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
example:-chair, bike, marker, pen, table, car, etc.
Class
Field
Method
Class: A class is a group of objects which have common properties. Its like a blueprint of the object. Before we create an object, we need to first create a class
Field:
Used to store data
Method:
Used to perform some operations
Syntax for class
class ClassName { // fields // methods }
Instance variable
Variable which is created inside the class but outside the method. It gets the memory at runtime when object/instance is created, that is why it is known as instance variable
What can a class contain in Java
A class in Java can contain:
1. Fields 2. Methods 3. Constructors 4. Blocks 5. Nested class and interface
new keyword
The new keyword is used to allocate memory at runtime
Constructor
Definition, syntax
In java constructor is just like a method but without a return type
Syntax:- class Test{ //creating a constructor Test(){ //constructor of the body } }
Note:- constructor name should be same as that of the class name
return type
Return type is basically a data type declared as a part of the function
Example:- nt multiply(int a,int b)
{
int c=a*b;
return(c);
}
Here int in a return type
Types of Java constructor
- Default constructor(no-arg constructor)
2. Parameterized constructor
Constructor overloading
It is a technique in Java of having more than one constructors with different parameters
Each constructor performs a different task
They are differentiated by the compiler by the number of parameters in their lists and their types
Constructor vs Method
- No return type
- Return type
- Used to initialize the state of the object(combination of the original values and the modifications made to them)
- A method is used to expose the behavior(function) of the object
- Invoked implicitly
- Invoked explicitly
Method
Collection of instructions that performs a specific task.
It provides the re usability of the code
It provides easy modification and readability of the code, just by adding and removing chunk of code.
The method is executed only when we invoke it
Method declaration
It has six components that are known as method header
1. Method signature :-
It includes method name and parameter list
2. Access specifier/modifier:- return type of the method. Specifies the visibility of the method. Java provides four types of access specifier: 1. public 2. private 3. protected 4. default 3
- Return type:-
data type that the method returns - Method Name: It is a unique name that is used to define the name of a method. A method is invoked by its name.
- Parameter List: It contains the data type and variable name. optional
- Method Body: It contains all the actions to be performed