OOPS
OOPS
Programming
-VINOTH S P
• OOPS is stands of Object Oriented Programming.
• The below are also known as four pillars of the object oriented
programming paradigm.
• abstraction, encapsulation, inheritance, and polymorphism.
Classes
• Java class is the definition block used to define state and behaviour of
the object.
• Java classes contains data members and member functions.
• Data members used to define the state of the object and member
functions are used to represents the behaviour of the objects.
• A class the piece of program which must be within pair of curly
braces.
Anatomy of a class
• JVM starts execution on main method while executing the java class.
Objects
• Objects are entities that having its own state and behavior.
• These are created using new keyword with any class type.
• Objects are knows the instant state and behavior of the class.
• Objects are real world entities.
Static and non static members
• Static members are associated with the class so it is called as a class
level members
• Non static members are associated with the instance of the class so it
is called as the instant members.
Static Non Static
Only single copy present in Static method. Multiple copies are exist in the Non Static.
It is accessed by using the class name. It is accessed by using the object reference variable.
These are also called as class members. These are called as a instance level member.
These are declared with the static keyword These are not declared with the static keyword
Static methods are loaded at the time of class loading Non static methods are loaded at the time of object
creation.
Blocks
• Blocks are used for initialization.
• There are two types of blocks are there,
1. Static blocks,
2. Non-Static Blocks.
Static data members
• Static data members are used to initialize the static data members,
these are loaded at the time of class loading.
• Global primitive have default values, not mandatory to initialization.
We can write any number of static block, It executed sequentially.
Class Stat{
Static int x;
Static {
x = 10;
}}public static void main(String args[]){System.out.println(x);})
Non Static init blocks
• It is also known as instance initialization blocks, these are declare without static
keyword.
• It is executed at the time of instantiation(Object creation time).
• Class Yes{
Public int s;
{
s = 17;
}
Public static void main (String args[]){
Yes y = new Yes();
System.out.println(y.s);
}}
Constructors
• Constructors are the special members of the class, that used initialize
the non static data members of the class.
• Constructors are not having any return type. If we specify any return
type it act like method.
• Constructors are having same name of class.
• It is used for creation of objects.
• Every class must have the constructor. Default constructor written by
the constructor when the programmer not used constructor in the
program.
User defined constructor types
• Zero argument constructor -> It is written by the programmer without
any argument, this constructor is created.
• Parameterized constructor -> We can pass the values to the
parameterized constructor in the form of arguments.
Constructor overloading
• The concept of developing multiple constructors for a class but differ
in argument is called as constructor overloading.
• Constructors are differ in the,
• Type of argument
• Length of argument
• Sequence of argument
Keywords
• Keywords are the reserved words that have the pre defined meaning.
Programmer cant able to change the pre defined meaning for the
keywords.
• These are only available in lower case keywords. There are more than
50 keywords are available in java.
• Ex : class, public, int, synchronized, Boolean, etc…
Variables
• Variables are the name for the particular memory address.
• Types:
• Local variable
• Instance variable
• Static variable
Methods / Functions
• A basic unit of writing the java program that is used to implement the
particular task, which is reusable.
• Syntax:
• <access specifier><modifier> return type methodname (<args>){}
• Access specifier for the visibility
• Modified indicates the nature. Ex : static, final, synchronized,abstact
• Return type is primitive or class type or array
• One method can be called at any method body.
• Static methods are called using class name.
• Non static members are called using the object reference type.
• Recursion : A method calls itself, called method call-back the calling
method.
Method Overloading
• The concept of creating the method with same name but differ in
argument list or type is called as method overloading.
• Differ in Type/Length/Sequence.
• Type :
• Void m1() void m1(int s) void m1(String s)
• Length:
• Void m2(), void m2(int s), void m2 (int x, String y)
• Sequence
• Void m3(int x, double y) void m3(double x, int y)
Method Overloading
• We can also overload the static methods.
• Private methods are overloaded but not accessed outside the class.
• With method overloading we will achieve the compile time
polymorphism.
Method Overriding
• The concept of changing the implementation of the super class with
the method of subclass in inheritance is called as method overriding.
• To achieve the method overriding we must have inheritance.
• We cant override the member declared as final.
• We cant override the private members because they are not
accessible in inheritance.
• Using method overriding we will achieve the runtime polymorphism.
Final Keyword
• It is used to create the constants in java. We cant able to change the
value.
• We cant able to inherit the final class.
• We cant override the final method.