OPP Concept

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Paradigm

Paradigm means a method or different approach to solve the problem using


some programming language.
Types: - OOP, Procedural paradigm, functional paradigm, logical paradigm,
structural paradigm.

Class in java
1.Class is just a template or blueprint that is used to categorized different
object and class can contain different methods or functions which server as the
behaviors or action that objects created from that class can performs.
2.Example a person class that have different attributes such as name , age, and
occupation along with methods like “introduce” and “update occupation”.

Objects in java
An object is basically an instance of class that has its own identity,
state/Attributes and behavior (means methods).
There are three steps of declaring variables:-
1.Declaration:-

2.Instantiation:-

3.Intialization:-
Constructors in java
In Java, Constructor is a block of codes similar to the method. It is called when
an instance of the class is created. At the time of calling the constructor,
memory for the object is allocated in the memory. It is a special type of
method that is used to initialize the object.
Every time an object is created using the new() keyword, at least one
constructor is called.
How Java Constructors are Different from Java Methods?
 Constructors must have the same name as the class within which it is
defined it is not necessary for the method in Java.
 Constructors do not return any type while method(s) have the return
type or void if does not return any value.
 Constructors are called only once at the time of Object creation while
method(s) can be called any number of times.

Need of constructor in java


In java constructor is needed to initialize the object when it is created.
If we don’t use constructor to initialize the object then we need to write
multiple line of code separately to initialize the object.

There are two types of constructors in Java:

1. Default constructor (no-arg constructor):- A constructor is called "Default


Constructor" when it doesn't have any parameter or which is by default created by
the compiler.
2. No-argu constructor (user defined):- no argument is provided.
3. Parameterized constructor: -A constructor that has parameters is called …. This
constructor is used when a programmer wants to initialize the object with
certain arguments at a time of object creation.
4.

Inheritance in Java?

Inheritance is a property by which a class can inherit or acquire the method or


function of other class or we can say that a object from a child class can acquire
all the properties and behaviour of a parent class.

Constructor and private method of a particular class cannot be inherited from one
class to another.

Why java does not support multiple inheritance?

Consider a case where class B extends class A and Class C and both class A and C
have the same method display (). Now java compiler cannot decide, which display
method it should inherit. And it will throw a error of ambiguity To prevent such
situation, multiple inheritances is not allowed in java.

Advantage and disadvantage.

1.Code reusability, code redundancy.

2.if method of a parent class is changed then it will affect its all-child class.
Association in java defines the connection between two classes that is
established or setup with their objects and it is called as Has-a relationship.

Aggregation

A kind of association where the entities are not strongly dependent on each
other

For example, let’s take two class one is car and other one is music player. A
car can exist without the music player and music player can also exist without
the car so both the classes are independent and they form weak relation
between them.

Composition: -car and engine example. They form strong relation between
them.
A kind of association where the entities are strongly dependent on each other or we can say
one alone cannot exist without other.

Polymorphism
Polymorphism in Java refers to an object capacity to take several forms. Performs allows us
to perform a single action in different ways.

Compile time polymorphism

Compile-time polymorphism is a polymorphism that is resolved during the compilation


process and it is implemented using the method overloading process.

Method overloading:-In java, compile time polymorphism is achieved by method


overloading.when a class has multiple methods with the same name but difference in
arguments:-1.No of argument or sequence of argument or type of argument any one of
these condition should be satisfied to achieve the method overloading. Depending on the
arguments used during the method call, the compiler chooses the right method to call.

Benefits and Use Cases:


Code Reusability: By allowing developers to reuse method names, method overloading
improves code readability and cuts down on redundant code.

The following are the key advantage of compile-time polymorphism:


 Debugging the code becomes easier.
 It helps us to reuse the code.
 Binding of the code is performed during compilation time.
 Overloading of the method makes the working of the code simpler and more
efficient.

Facts about method overloading.


1.by changing return type we cannot achieve method overloading because of
ambiguity.
2.can we overload java main() method?
-yes, we can overload main method, jvm will call first method with string
argument then other method.
3.study about var argument which is added in java1.5.

Automatic Promotion

The name Type


Promotion specifies that a small size datatype can be promoted to a large size
datatype. i.e., an Integer data type can be promoted to long, float, double, etc.
This Automatic Type Promotion is done when any method which accepts a
higher size data type argument is called with the smaller data type.

Run Time Polymorphism in java.


It is a process in which function call to an overridden method is resolved
during the run time rather than compile time.
This type of polymorphism is achieved by method overriding.
Method overriding: - if subclass or child class has the same method as
declared in the parent class, it is known as method overriding in java.

Rules for Java 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).

What is the use of the method overriding in java?

Method overriding allows a subclass or child class to run their own


method that is already present by one of its superclasses or parent
classes.

Some question:

1.Do overriding methods must have same return type?

-From java 1.5 onwards it is possible to have different return type for a
overriding methods in child class, but child return type should be sub-
type of parents return type. Object (parent class) -string(child class).

2.Overriding and access-modifiers


A child class should have higher access modifier than the parent’s class
or both can be equal in order.

For example if child class has access modifier public then parents class
should have access modifier default.

Eiether both can default/public …..parents class should always have


access modifier lower order than child.

Case-5 Invoking overridden method from sub-class

-By using super keyword, we can call parent class method by using child
class object.
In Java Abstraction is the property by which only the essential details are
displayed to the user. The trivial or the non-essential units are not displayed to
the user.

Abstraction can be achieved by two methods: -

1.Abstract class

1. A method without body (no implementation) is known as abstract method.

2.If a class contain an abstract method then it should always be a abstract class but it is not
necessary that abstract class should contain a abstract method.

3.If a regular class extends an abstract class, then it compulsory to create the body of
abstract method present in abstract class.

4.we cannot create object of abstract class because then we can also call the method of
abstract class but it does not contain body so it is of no use.
2.Interface: -They are similar to Abstract Class but having all the methods of
abstract type.

Why use Java interface?


o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.

How to declare an interface?


An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the
variables or field created are public, static and final by default. A class that
implements an interface must implement all the methods declared in the interface.

We can create method body of interface class by static and default keyword.

And by default, the compiler adds public abstract to any methods.


Encapsulation in Java
Encapsulation in Java is a process of wrapping data and code acting on the data
which are methods together as a single unit . for example, a capsule which is mixed of
several medicines.

We can create a fully encapsulated class in Java by making all the data members of
the class private. Now we can use setter and getter methods to set and get the data
in it.

In encapsulation the variables of a class will be hidden from other classes, and can be
accessed only through the methods of their current class. This concept is known as
data hiding.

Advantage of Encapsulation in Java


By providing only a setter or getter method, you can make the class read-only or write-
only. In other words, you can skip the getter or setter methods.

It provides you the control over the data. Suppose you want to set the value of id
which should be greater than 100 only, you can write the logic inside the setter
method. You can write the logic not to store the negative numbers in the setter
methods.

It is a way to achieve data hiding in Java because other class will not be able to
access the data through the private data members.

The encapsulate class is easy to test. So, it is better for unit testing.
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object.

If we don’t use this keyword and we try to put local variable in instance
variable having same name then it will print 0 because local variable value
was not able to fetch in instance variable.
But if we use this keyword then it will refer to the instance variable of class
and local variable (same name) value will be stored in instance variable.

2.Using this () to invoke current class constructor.

class Test {
int a;
int b;

// Default constructor
Test()
{
this(10, 20);
System.out.println(
"Inside default constructor \n");
}

// Parameterized constructor
Test(int a, int b)
{
this.a = a;
this.b = b;
System.out.println(
"Inside parameterized constructor");
}

public static void main(String[] args)


{
Test object = new Test();
}
}

Output
Inside parameterized constructor
Inside default constructor

3.Use ‘this’ keyword as a method parameter.

class Test {

void m1(Test t){


sout(“this is m1”);
}

Void m2(){
m1(this);
}
// main function
public static void main(String[] args)
{
Test object = new Test();
Object.m2();
}

Output: -it will print “I am m1”.

This and Super keyword in java difference


This keyword is a reference Super keyword is a reference
variable which is used to refer variable which is used to refer
current class object. immediate parents class object.
In order to call the default constructor of the
current class, we can use this keyword. In order to call the default constructor of the
parent class, we can use the super keyword.
We can use it to access only the current class We can use it to access the data members
data members and member functions. and member functions of the parent class.
Output: - “I am in class A”

“I am in class B”.

Final Keyword in Java


The final keyword in java is used to restrict the user. The java final keyword can be
used in many contexts. Final can be:

1. variable
2. method
3. class

1) Java final variable


If you make any variable as final, you cannot change the value of final variable (It will
be constant).

It will show compile time error if we change the value.

It is used to store some constant variable like Pi, Gravity or value which we do not
want to be changed.

2) Java final method


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

1. class Bike{
2. final void run(){
System.out.println("running");
}
}
3. class Honda extends Bike{
void run(){
System.out.println("running safely with 100kmph");
}

4. public static void main(String args[]){


Honda honda= new Honda();
honda.run();
}
5. }
3) Java final class
If you make any class as final, you cannot extend it.

1. final class Bike{}


2.
3. class Honda1 extends Bike{
4. void run(){System.out.println("running safely with 100kmph");}
5.
6. public static void main(String args[]){
7. Honda1 honda= new Honda1();
8. honda.run();
9. }
10. }
11. Output:Compile Time Error

Static Variable in java

1.staic variable should always be created at class level not at local level.

2.static variable belong to the class not an object. we can call directly without
creating the object.

3.Static variable are used for memory management. When a variable is declared as
static, then a single copy of variable is created and shared among all objects at class
level.

4.In Java, the ‘static’ keyword is used to define (variable and methods) that belongs
to the class rather than to instance of class.

Static Variables (Class Variables): When a variable is declared as static within a class, it
becomes a class variable. It is shared among all instances of the class. Only one copy of the
static variable exists, and it is created when the class is loaded.

Static method:-When a method is declared as static, it belongs to the class not the object or
instances of class. Static methods can be called using the class name without creating an
instance of the class.

Static method can only called static method and cannot call a non-static method.
1.If the main method is within the class, then no need to do class. method name to call the
method it can be directly called by method name

Static block: - In Java, a static block is a special block of code inside a class that gets
executed only once when the class is loaded into memory. It's used for performing some
one-time initialization tasks for the class or setting up static variables.

You might also like