OBJECTS
Object
• Anything which is present in the real world and physically existing
can be termed as an Object.
• Ex: Car,laptop,Tv etc…….
The Properties of every object is categorized into 2 types:
1. States
2. Behaviours
• States are the properties used to store some data.
• Behaviour are the properties to perform some task.
OBJECTS
Note:
• Every object will have its own properties & behaviours.
• Since the states & behaviours belongs to one particular object , If
we need to store all these things we need one dedicated memory
location(Container).
• But we don’t have any predefined datatype which helps to create
such container.
• Hence we can design our own datatype with the help of a class.
Class
• class is a blue print of an object.
• It’s a platform to store states and behaviors of an object.
• class has to be declared using class keyword.
• We need class for execution of program.
• In a class we can create members like methods( to store behaviors
of an object) as well as variable(to store the states of object).
CLASS
Ex:
class Employee
{
// design variables to store states.
// design methods to store behaviors.
}
CLASS
Members of Class:
• Anything which is declared within class block is known as
members of the class. Ex: Methods, Variables, Constructors
etc….
• A class will have 2 types of members
1. Data members
2. Function members
• The members of the class can be classified into 2 Types..
1. Static Members of class.
2. Non-Static Members of class.
Static Members of class:
• Any members of the class which is prefixed with static modifier is known
as static member of class. In java we have Following static members.
– Static Method
– Static Variable
• Static Methods:
• -A method which is prefixed with static modifier is known as static
methods.
• -A block which belongs to a static member is known as Static Context.
• Static Variable:
• -A variable which is prefixed with static modifier / keyword is known as
Static Variable.
• -It should be declared inside class block.
How to use a static members of one class inside static context
of same class ?
• Static member present in same class can be accessed just by
using memberName.
• How to use a static members of one class inside static
context of another class ?
• Static member present in different class can be accessed by
using class name with member name.
• Ex : ClassName.memberName
• Access Operator(.) :
• *) It is a Binary Operator.
• *) It is used to access a member present in another class.
public class Student
{
static int age = 20;
public static void study()
{
System.out.println("Student is Studying");
}
public static void main(String[] args)
{
study();
System.out.println(" ");
System.out.println(age);
}
}
o/p:
Student is Studying
20
class Employee
{
static int id = 101;
public static void work()
{
System.out.println("Employee is Working");
}
}
public class Test
{
public static void main(String[] args)
{
System.out.println(Employee.id);
Employee.work();
}
}
o/p:
101
Employee is Working
Non-Static Members of class:
• -Any member declared inside class block without prefixing static
modifier is known as Non-static member of a class.
• -In java we have following Non-static members
– Non-static Variable.
– Non-static Method.
Note:
• -The non-static members gets their memory allocated inside object.
• -If we need to access any non-static members…. The first thing we
must do is create an object.
What is Object?
• An object is a block of memory created during in runtime inside heap
area.
• The object also known as Instance Of A Class.
• The process of creating an object is known as INSTANCIATION.
• Syntax to create an object: new ClassName();
• new is a keyword.
• new will create a block of memory inside heap area and it will return
the address of it.
• The type of address generated for the object will always be specific to
its class.
• How to use a non-static members inside static context of
same/different class ?
• Non-Static member present in a class can be accessed within a static
method only by creating the object of the class.
#Accessing Non-Static Variables inside same class
class Student
{
// NON-STATIC VARIABLES
int age = 19;
String name = “ISE";
public static void main(String[] args)
{
System.out.println("start");
System.out.println(new Student().age);
System.out.println(new Student().name);
System.out.println("end");
}
}
o/p
start
19
ISE
end
Inheritance Concepts in Java
An overview of Java OOP Inheritance
What is Inheritance?
• Mechanism where one class inherits from another.
• Supports 'is-a' relationship.
• Child class reuses fields and methods from parent.
Types of Inheritance in Java
• Single Inheritance – Supported
• Multilevel Inheritance – Supported
• Hierarchical Inheritance – Supported
• Multiple Inheritance (with classes) – Not supported
• Multiple Inheritance (with interfaces) – Supported