Java Part 2
Java Part 2
Fundamental of oops:
Object: It is a Block of Memory which can have multiple variables
and methods in it.
*) Every Object will have reference(Address).
*) An Object represents realworld entity/object.
*) It can store Hetrogeneous type of data.
Realworld Object: Anything which has existence in real world is
known as Realworld object.
Ex: Car,laptop,Tv etc…….
*) Every object will have its own properties & behaviours.
Note: Since the states & behaviours belongs to one particular object
, If I 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,
Which can be used as non-primitive datatype.
Class:
*) Class is a keyword.
*) It is used to design a user defined non-primitive datatype to store
the details of object.
*) It can also be considered as blueprint of an object.
*) We need class for execution of program.
*) In a class we can create members like methods( to store
behaviours of an object) as well as variable(to store the states of
object).
*) Every classname can be a Non-primitive datatype.
*) The java language also comes up with lot of built-in classes
i.e, String, System etc……
Ex: class Employee
{
// design variables to store states.
// design methods to store behaviours.
}
Note:
*) The non-primitive datatype will have only default values i.e null.
*) They will not be having any default size.
*) Since the name of the class itself can be used as a non-primitive
datatype.
*) we can have any members of non-primitive datatype in java.
Members of Class: Anything which is declared within class block is
known as members of the class.
Ex: Methods, Variables, Initializers, Constructors etc….
The members of the class cab be classified into 2 Types..
1.) Static Members of class.
2.) Non-Static Members of class.
Access Operator(.) :
*) It is a Binary Operator.
*) It is used to access a member present in another class.
Points to be Remember:-
1.) A static variable gets its memory allocated inside class static area.
2.) Since all static variables will be present inside class static area,
they are also known as Class Variables.
3.) A static variable will be assigned with default values.
4.) A static variable can be used anywhere within the class also in a
different class.
i) same class: Directly using variable name.
ii) diff class: With the help of classname as reference.
5.) A static variable of one class can be accessed inside another class
with the help of classname.
6.) In java we can have local variable as well as global variable with
the same name.
7.) In such situations the highest priority will be given the local
variables.
8.) Global variable can be used inside all the members of the class.,
whereas the local variables can be used only inside the block where
it is declared.
iii) Static Initializers: Initializers are the members of the class which
are used for initialization purpose.
In java we have two types of Initializers:
1) Static declare & Initialization statement.
2) Static block.
Note: Inside a class we can have more than one static block & they
gets executed in top to bottom order.
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 id 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 constructor of the class;
new:
*) new is a keyword.
*) It is a unary operator.
*) It is used to create an object along with constructor of the class.
*) new will create a block of memory inside heap area and it will
return the address of it after the loading process is completed.
*) The type of address generated for the object will always be
specific to its class. The address of object generated will always be in
the below format:
classname@hexadecimal value
Constructor:
Constructor are used to load the non-static members into the object.
Note:
1.) We can have multiple objects created for one single class.
2.) Everytime we create a new object, it will create a new block of
memory with different address.
3.) Everytime we create an object , the non-static members will be
loaded into an object.
Non-Primitive Datatypes:
*) They are user defined datatypes which are used to store the
following things:
i) The default values of non-primitive datatypes i.e null.
ii) Address of an object.
*) The variables which are created using non-primitive datatypes is
known as Non-primitive variables / Reference Variables.
Note:
Inside static context we can access static members in 3 ways:
i) Directly with the help of member name.
ii) With the help of classname & access Operator.
iii) With the help of address of the object.
Inside a static context we can access non-static members in
following ways:
i) Only with the help of Address of the object.
i) Non-static Variable: A variable which is not prefixed with static
modifier is known as non-static variable.
*) They gets their memory allocated inside object created in heap
area.
*) Non static variable can be accessed with the help of object
reference or address.
*) Non static variables will be allocated in every object of the class
created.
*) Non static variables initialized with the default values.
Constructor:
Constructor is a special non-static members which is used to load all
the non-static members of a class into the object.
Note:
1.) The name of the constructor must be same as that of classname.
2.) The constructor is just similar to a method except the return
type.
3.) Constructor doesn’t return anything after the execution, hence
constructor has no returntype.
4.) In java every class created must and should have a constructor.
5.) Therefore, the compiler will always check whether a programmer
has created atleast one constructor inside the class or not.
6.) If it is not created , then compiler will add a constructor into the
class file such constructors are known as Default Constructor.
Types of Constructors
1.) Default Constructor
2.) User-Defined Constructor.
i.) No-Argument Constructor.
ii.) Parameterized Constructor.
1.) Default Constructor: A No-argument constructor added by the
compiler during the compilation is known as Default Constructor.
*) It will be added only when the user forgets to create even a single
constructor inside the class.
Data Hiding: The Process of restricting the direct access to the states
of the class but providing controlled access with the help of
behaviours of the same class is known as Data Hiding.
*) Data Hiding is possible only inside encapsulated class.
Private:
*) It is a Keyword.
*) It is a access modifier.
*) Any members which is prefixed with private access modifier can
be accessed only within the same class.
1.) Getter Method: It is a Method which is used to print the private
members data from different class.
*) It returns the data of members which has to be read.
Syntax: Datatype of private_Mem getPrivateMemName()
{
Return Private_Mem_name;
}
Types of Inheritance
1.) Single-level Inheritance
2.) Multi-level Inheritance
3.) Hierarchical Inheritance
4.) Multiple Inheritance
5.) Hybrid Inheritance
Note:
*) By default interface is an abstract member.
*) we cannot create an Object for interface.
*) we can compile an interface & generate a class file for that.
*) we cannot make an interface either private or protected, It can be
either default or public.
*) we cannot make an interface as final member.
*)An Interface can inherit more than one interfaces with the help of
extends keyword.
Case2: A class inherits interface.
i) A class can inherit Interface using implements keyword.
*) implements is represented using dotted line.
Ex: interface A1
{
}
Class B implements A1
{
}
ii) A class can inherit any number of interfaces.
Ex: interface A1{ interface A2{
} }
Class B implements A1,A2{
}
3.) A class can inherit one class & any no. of interfaces.
Ex: interface A1{} interface A2{} class A3{}
Class B extends A3 implements A1,A2{}