Introduction Classes and Objects
Introduction Classes and Objects
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.
• 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.
• When Java programs run on the JVM, objects are created on the heap,
which is a portion of memory dedicated to the program.
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.
2.Parameterized constructor
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.
• If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.
• 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();
}
}