OPP Concept
OPP Concept
OPP Concept
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.
Inheritance in Java?
Constructor and private method of a particular class cannot be inherited from one
class to another.
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.
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.
Automatic Promotion
Some question:
-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).
For example if child class has access modifier public then parents class
should have access modifier default.
-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.
1.Abstract class
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.
We can create method body of interface class by static and default keyword.
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.
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.
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");
}
Output
Inside parameterized constructor
Inside default constructor
class Test {
Void m2(){
m1(this);
}
// main function
public static void main(String[] args)
{
Test object = new Test();
Object.m2();
}
“I am in class B”.
1. variable
2. method
3. class
It is used to store some constant variable like Pi, Gravity or value which we do not
want to be changed.
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");
}
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.