0% found this document useful (0 votes)
20 views

Java OOPS Documentation

The document discusses the key concepts of object-oriented programming in Java including classes, objects, encapsulation, inheritance, polymorphism, abstraction and more. It provides definitions and examples for each concept.

Uploaded by

Deep Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java OOPS Documentation

The document discusses the key concepts of object-oriented programming in Java including classes, objects, encapsulation, inheritance, polymorphism, abstraction and more. It provides definitions and examples for each concept.

Uploaded by

Deep Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java OOPS Documentation

What is OOPS?

Ans:

In java OOPS is refers object oriented programming it is used to implement real


world entities like inheritance, encapsulation, abstraction and polymorphism.

What is Class?

Ans:

- In Java, a class is a blueprint for creating objects.


- It defines the properties and methods
- Example, Animal Class which has properties like name, type, colour and
methods like eat(), run() etc.

What is Object?

Ans:

- Objects are instances of a class.


- We can create object by new keyword.

What is Encapsulation?

Ans:

- Encapsulation in Java is a process of binding data together into a single


unit.
- We can implement encapsulation using access modifier by making private
properties.
- We can provide access to its properties through methods such as getters and
setters.

What is Inheritance?

Ans:

Inheritance is process of acquiring methods and properties from one class to another
class.
Types:

- Single inheritance
- Multi-level Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance (mix-up of above all types)

- Single Inheritance: In single inheritance, a child class inherits properties and


methods from a single parent class.

- Multiple Inheritance: in multiple inheritance one child class inherits method


and properties from more than one parent class.

- Multilevel Inheritance: In multilevel inheritance one class inherits properties


and methods from its parent class which is also a child class of another parent
class.

- Hierarchical Inheritance: in hierarchical inheritance more than one child class


inherits method and properties from single parent class.
What is abstraction?

Ans:

Abstraction in Java is a process of hiding the implementation details from the user
and showing only the functionality to the user.

- We create abstract class using abstract keyword

- It can have abstract and non-abstract methods.


- We can not create object of abstract class.
- Example, Instagram provide a functionality of post, reels but we don’t know
the internal working details how it works.

There are two ways to achieve abstraction in java

1. Abstract class (0 to 100%)


2. Interface (100%)

What is Polymorphism?

Ans:

Polymorphism is used to perform a single task in different ways.

The word "poly" means many and "morphs" means forms.

- There are two types of polymorphism


o Runtime polymorphism:- Method overriding
o Compile time polymorphism:- Method overloading
Method Overriding:

1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).

Example

public class Animal {

public void makeSound() {

System.out.println("Animal makes a sound.");

public class Dog extends Animal {

@Override

public void makeSound() {

System.out.println("Dog barks.");

public class Cat extends Animal {

@Override

public void makeSound() {

System.out.println("Cat meaw.");

}
Method Overloading

- If class has more than one method which has same name but different signature
it is called method overloading.
- Signature means,
o It can have different Return type
o It can have different parameters
o But Name should be same

Example

class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
static double add(double a, double b){
return a+b;
}
}

What are the types of variables in OOP?

Ans:

Instance Variable(reference variable):

- it is an object level variable like we gave reference name to array, List or our
user defined object.

Static Variable

- The static variable can be used to refer to the common property of all objects for
example, the company name of employees, college name of students
- The static variable are define inside class.

Local Variable

- A variable declared inside the body of the method or block is called local variable.
- A local variable cannot be defined with "static" keyword.
Why is the Java main method static?
Ans:

- It is because the object is not required to call a static method.


- If it were a non-static method, JVM creates an object first then call main()
method that will lead the problem of extra memory allocation.

Pass By Value & Pass By Reference

Ans:

Pass By Reference

- Jub bhi hum object create karte tab uska reference stack me store hota hai or
uski value heap store hoti hai.
- Agar me is object ko kisi method me pass karta hu to ek new reference create
hota hai jo same object ko point karta hai.
- Agar me iske through koi bhi changes object karta hu to vo changes original
me bhi hota hai.

Pass By Value

- Primitive data type value store inside stack memory.


- Agar me primitive value method me pass karta hu to uska new variable stack
memory me create hota hai agar me isme koi bhi change karu to vo usi me hi
rehta hai original me nahi hota.
Constructor

- Constructor is automatically called when we create an object.


- Constructor has same name as class name.
- Constructor does not have any return type.
- Constructor is used to initialize properties of an object.
-

Final KeyWord

• Method

- If you make any method as final, you cannot override it.

• Variable
- If you create any variable with final keyword then you can not change its
value.
• Class
- If you make any class as final, you cannot extend it.

Autoboxing & Unboxing Wrapper Class

autoboxing and unboxing feature convert primitives into objects and objects into
primitives automatically. The automatic conversion of primitive into an object is known as
autoboxing and vice-versa unboxing

Recursion in Java
- Recursion in java is a process in which a method calls itself continuously. A
method in java that calls itself is called recursive method.

Array

Array is a collection of similar type of elements which has contiguous memory location.
Super Keyword
The super keyword in Java is a reference variable which is used to refer immediate parent
class object.

Usage of Java super Keyword


1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

Packages

A java package is a group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two form, built-in package and user-defined
package.

Data Types in Java


Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:

1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.

You might also like