OOPS concepts
OOPS concepts
Object
• Any entity that has state and behavior is known as an object. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical.
• An Object can be defined as an instance of a class. An object contains
an address and takes up some space in memory.
• Example: A dog is an object because it has states like color, name,
breed, etc. as well as behaviors like wagging the tail, barking, eating,
etc.
Class
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which you can create
an individual object. Class doesn't consume any space.
• For example, Student is a class while a particular student named Ravi
is an object.
public class A
{
String name;
float salary;
void set(String n, float p)
{
name = n;
salary = p;
}
void get()
{
System.out.println("Employee name is: " +name );
System.out.println("Employee CTC is: " + salary);
}
public static void main(String args[])
{
A obj=new A();
obj.set(“ankit kumar", 10000.0f);
obj.get();
}
}
Local, Instance and Static Variables
• There are three types of variables in java: local, instance and static.
Instance Variable
• A variable declared inside the class but outside the body of the
method, is called an instance variable. It is not declared as static.
• It is called an instance variable because its value is instance-specific
and is not shared among instances.
Properties of instance variable
Cont..
Local Variable
• A variable declared inside the body of the method is called local variable.
• You can use this variable only within that method and the other methods in
the class aren't even aware that the variable exists.
• A local variable cannot be defined with "static" keyword.
• It is not accessible outside the method.
• It do not get any default value.
Static Variable
• A variable that is declared as static is called a static variable.
• It cannot be local. You can create a single copy of the static variable
and share it among all the instances of the class.
• Memory allocation for static variables happens only once when the
class is loaded in the memory.
Example:
public class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
int n=90;//local variable
System.out.println(n);
}
public static void main(String args[])
{
A obj=new A();
System.out.println(obj.data);
System.out.println(A.m);
obj.method();
}
}
Default Values to these variables
class A
{
int a;
static int b;
void fun()
{
int c;
System.out.println(a+" "+b+" "+c);
}
public static void main(String args[])
{
A obj=new A();
obj.fun();
}
}
Garbage Collection
• Garbage collection in Java is the process by which Java programs perform
automatic memory management.
• Java programs compile to bytecode that can be run on a Java Virtual
Machine, or JVM for short.
• When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program.
• Eventually, some objects will no longer be needed. The garbage collector
finds these unused objects and deletes them to free up memory.
Array of Objects
Class_Name[ ] objectArrayReference;
or
Class_Name objectArrayReference[];
Example:
Student obj[] = new Student[2];
Constructor
• A 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 constructor, memory for the object is allocated
in the memory.
• It is a special type of method which is used to initialize the object.
Rules for creating Java constructor
There are two rules defined for the constructor.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized
Types of Java constructors
There are two types of constructors in Java:
1.Default constructor (no-arg constructor)
Explicit Default constructor
Implicit Default constructor
2.Parameterized constructor
Default Constructor
• A constructor is called "Default Constructor" when it doesn't have any
parameter.
Syntax of default constructor:
<class_name>()
{
}
• Explicit means something is done by the programmer
• Implicit means that its done by the complier.
Example of Explicit default constructor
class Bike1
{
Bike1()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike1 b=new Bike1();
}
}
Example of Implicit default constructor
class Student3
{
int id;
String name;
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Parameterized Constructor
• A constructor which has a specific number of parameters is called a
parameterized constructor.
Parameterized Constructor
class Student4
{
int id;
String name;
Student4(int i, String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Constructor Overloading
public class Student
{
int id;
String name;
Student()
{
System.out.println("this a default constructor");
}
Student(int i, String n)
{
id = i;
name = n;
}
public static void main(String[] args)
{
Student s = new Student();
System.out.println("Default Constructor values: ");
System.out.println("Student Id : "+s.id + "Student Name : "+s.name);
System.out.println("Parameterized Constructor values: ");
Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "Student Name : "+student.name);
}
}
Polymorphism
• Polymorphism is derived from two Greek words, “poly” and “morph”,
which mean “many” and “forms”, respectively.
• Polymorphism is the ability of a message to be displayed in more than
one form.
Types of Java polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism
Compile-Time Polymorphism
• It is also known as static polymorphism. This type of polymorphism is
achieved by function overloading or operator overloading.
• But Java doesn’t support the Operator Overloading.
Method Overloading
• If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
Program 1 of Method Overloading
class A
{
static int multiply(int a, int b)
{
return a * b;
}
static double multiply(double a, double b)
{
return a * b;
}
}
class B
{
public static void main(String[] args)
{
System.out.println(A.multiply(2, 4));
System.out.println(A.multiply(5.5, 6.3));
}
}
Program 2 of Method Overloading
class A
{
static int multiply(int a, int b)
{
return a * b;
}
static int multiply(int a, int b, int c)
{
return a * b * c;
}
}
class B
{
public static void main(String[] args)
{
System.out.println(A.multiply(2, 4));
System.out.println(A.multiply(2, 7, 3));
}
}
Runtime Polymorphism
• It is a process in which a function call to the overridden method is resolved
at Runtime. This type of polymorphism is achieved by Method Overriding.
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Upcasting
• If the reference variable of Parent class refers to the object of Child
class, it is known as upcasting.
class A{}
class B extends A{}
A a=new B();//upcasting
For upcasting, we can use the reference variable of class type or an
interface type
Program of Method Overriding
class Parent
{
void Print()
{
System.out.println("parent class");
}
}
class subclass1 extends Parent
{
void Print()
{
System.out.println("subclass1");
}
}
class subclass2 extends Parent
{
void Print()
{
System.out.println("subclass2");
}
}
class A
{
public static void main(String[] args)
{
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}