OPPs Notes
OPPs Notes
What is OOPs?
Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and
logic. An object can be defined as a data field that has unique attributes and
behaviour.
What is a class?
A class is a collection of objects. Classes don’t consume any space in the
memory.
It is a user defined data type that act as a template for creating objects of the
identical type.
A large number of objects can be created using the same class. Therefore, Class
is considered as the blueprint for the object.
What is an object?
An object is a real-world entity which have properties and functionalities.
Object is also called an instance of class. Objects take some space in memory.
For eg .
• Fruit is class and its object s are mango, apple, banana
• Furniture is class and its objects are table, chair, desk
Object:
1. It is an instance of a class.
2. It takes space in memory.
3. Objects can be declared as and when required
Inheritance: This principle provides the ability to create new classes based on
the existing classes. It allows the derived class (subclass) to inherit the
properties and behaviours of the base class (superclass). Inheritance promotes
code reusability and supports the concept of "is-a" relationship.
Encapsulation:
The main advantage of encapsulation is that data is hidden and protected from
randomly access by outside non-member methods of a class. Encapsulation is
the process of binding data and methods in a single unit. In encapsulation,
data(variables) are declared as private and methods are declared as public
}
}
Inheritance:
Inheritance is the procedure in which one class inherits the attributes and
methods of another class.
In other words, It is a mechanism of acquiring properties or behaviours of
existing class to a new class
The Base Class, also known as the Parent Class is a class, from which other
classes are derived.
The Derived Class, also known as Child Class, is a class that is created from an
existing class
return arr;
}
return a;
}
}
}
Abstraction:
Allows to hide unnecessary data from the user. This reduces program
complexity efforts.
it displays only the necessary information to the user and hides all the internal
background details.
If we talk about data abstraction in programming language, the code
implementation is hidden from the user and only the necessary functionality is
shown or provided to the user.
In other words, it deals with the outside view of an object (Interface)
Eg.
All are performing operations on the ATM machine like cash withdrawal etc.
but we can't know internal details about ATM
phone call we don’t know the internal processing
Overloading
Method overloading means declaring multiple methods with same method
name but having different method signature.
In method overloading while writing the method signature we have to follow
following 3 Rules
• Method name must be same for all methods
• List of parameters must be different like different type of parameters,
different
number of parameters, different order of parameters.
• Return type is not considered in method overloading; it means we never
decide method overloading with return type
class Shape {
void draw()
{
System.out.println("Drawing Shape");
}
}
super keyword
In Java, super keyword is used to refer to immediate parent class of a child class.
In other words, super keyword is used by a subclass whenever it need to refer to
its immediate super class.
Note: When calling the parent class constructor from the child class using super
keyword, super keyword should always be the first line in the
method/constructor of the child class.
Q. Can we use both this () and super () in a Constructor?
NO, because both super () and this () must be first statement inside a
constructor. Hence, we cannot use them together.
Final keyword
final is a keyword or modifier which can be used at variables, methods &
classes.
If we declare a variable as final then we can’t modify value of the variable. The
variable acts like a constant. Final field must be initialized when it is declared.
If we declare a method as final then we can't override that method
If we declare a class as final then we can't extend from that class. We cannot
inherit final class in Java.
Type casting
Type casting is the process of converting value from one type to another type.
We always do typecast between 2 different data types which are compatible.
Note: In between 2 same data types typecasting is not required.
In java we have following 2 types of type castings
1.type casting w.r.t primitive data types
2.type casting w.r.t reference types
Type casting wrt primitive data types
Type casting wrt primitive data types means converting value from one primitive
data type to other primitive data type
Type casting can be done only between compatible data types
Compatible data types are byte, short, int, long, float, double, char
we have following 2 types of Type casting w.r.t primitive data types
1) Widening
2) Narrowing
Widening
Widening means converting the value from lower data type into higher data
type.
syntax:
higher datatype = (higher datatype) lower datatype ;
• Here data type specified in between the pair of parentheses is called type
casting.
• In widening writing the type casting is optional
• If we don’t write any type casting then compiler will write the type casting
automatically hence it is called as implicit type casting.
Narrowing
narrowing means converting the higher data type value into smaller data type.
syntax:
lowerdatatype = (lowerdatatype) higherdatatype;
-> In narrowing writing the type casting is mandatory
-> In narrowing if we don’t write any type casting then compiler won’t write any
typecasting
because there is a chance of loss of some data so that user has to write the
typecasting explicitly hence it is called as explicit type casting
Type casting w.r.t reference types
-> Type casting w.r.t reference types means converting the object from one
reference type to another reference type
-> But Type casting w.r.t references can be done only between compatible types
-> The two references said to be compatible if and only if its corresponding
classes having Is-A relation
-> Type casting w.r.t references is also classified into following 2 types
Up casting
-> Up casting means storing the child class object into the parent class reference.
syntax:
parentreferencetype = (parentreferencetype) childreferencetype
Note: In up casting writing typecasting is optional Down casting
-> Down casting means storing the Parent class object into the child class
reference.
syntax: childreferencetype = (childreferencetype) parentreferencetype
Note: In down casting writing typecasting is mandatory
Java Abstract class and methods
-> A class which is declared using abstract keyword known as abstract class.
-> An abstract class may or may not have abstract methods.
-> We cannot create object of abstract class.
-> It is used to achieve abstraction but it does not provide 100% abstraction
because it can have concrete methods.
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It is used for abstraction.
Syntax :
abstract class class_name { }
Abstract method
Method that are declared without any body within an abstract class are called
abstract method.
The method body will be defined by its subclass.
Abstract method can never be final and static.
Any class that extends an abstract class must implement all the abstract
methods.
Syntax:
abstract return_type function_name (); //No definition
this in Java
}
Types of variables:
1. Instance: ese variable jo class k ander or method k bahar ho
2. local: ese variable jo method k ander ho
3. static: ese variable jo static keyword se bane ho
Instance Variable:
1. declared inside a class but outside any method, constructor, or block.
2. Available to all methods, constructors, and blocks in the class.
3. Each object of the class has its own copy of the instance variables.
4. Created when an object is created using the new keyword and destroyed when the
object is destroyed.
Check code for below example:
Local Variable
1. Variables declared inside a method, constructor, or block.
2. Only accessible within the method, constructor, or block in which they are declared.
3. Stored on the stack and only exists during the execution of the method, constructor,
or block.
4. No default values. Must be initialized before use.
5. Access Modifiers: Cannot use access modifiers (private, protected, public) for local
variables.
Static Variable
1. Variables declared with the static keyword inside a class, but outside any method,
constructor, or block.
2. Belongs to the class rather than any object. All instances of the class share the same
static variable.
3. Stored in the static memory. Only one copy exists regardless of the number of
instances.