Abstraction: Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
Encapsulation: Encapsulation simply means binding object state (fields) and
behaviour(methods) together. If you are creating class, you are doing encapsulation.
Message passing
A single object by itself may not be very useful. An application contains many objects. One
object interacts with another object by invoking methods on that object. It is also referred to
as Method Invocation a diagram below.
Class
group of objects which have common properties.
template or blueprint from which objects are created.
logical entity it can't be physical.
It contain fields, Methods, Constructors, Blocks, Nested class and interface
Polymorphism
Polymorphism means single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means many forms.
two types of polymorphism in Java: static polymorphism or compile time binding or
early binding and runtime polymorphism or run time binding.
Polymorphism can be implemented by overloading and overriding process
Method Overloading
Class having more than one method having the same name but their argument lists are
different, return type may or may not be same depend on the parameter type
Its advantage is to increases the readability of the program
Example of static polymorphism where binding of method call to its definition at Compile
time.
int add(int a, int b)
int add(int a, int b, int c)
int add(float a, int b, double d)
float add(float f1,float f2)
e.g
class Overloading
{
public void display(char c)
{
System.out.println(c);
}
public void display(char c, int num)
{
System.out.println(c + " "+num);
}
public void display(String s, int num)
{
System.out.println(s + " "+num);
}
}
class Demo
{
public static void main(String args[])
{
Overloading o = new Overloading();
o. display('a');
o. display('a',10);
o.display(“sonam”,23);
o.display(.90f,23.4f); // if display(double d, double d) and we passed float value then
automatic conveted or promoted
}
}
Method Overloading and Type Promotion
One type is promoted to another implicitly if no matching data type is found
byte can be promoted to short, int, long, float or double e.g is highlighted in the above program
The char data type can be promoted to int, long, float or double
Invalid:
Same parameter with same data types
Different return type but same parameter
Object
is a real world or run time entity(not every time)
having two characteristics i.e state(properties) and its behaviour (actions which is
called method ) excluding the internal charactistic i.e identity
States can be represented as instance variables and behaviours as methods of the class
It can be physical or logical (tangible and intangible). The example of an intangible
object is the banking system.
instance of a class
Eg:
Object :Riya
State: color,height,looks etc.
Behaviours: go, run, performtask etc.
Identity: it is used internally by the JVM to identify each object uniquely.
Method
block of code an actual part of the program which perform task and exposed the
behaviour of an object
we can pass data, known as parameters, into a method
A method must be declared within a class also called instance method.
It is defined with the name of the method, followed by parentheses ().
Java provides some pre-defined methods, such as println(), but you can also create our
own methods to perform certain actions
class Demo
{ void Method_name()
{
// code to be execute
}
this
is a key word also known as this pointer is reference variable refers to the current
object.
is used to avoid the confusion between the variables used in class and the parameters
name of method or constructor are same
used to invoke current class constructor such as this.demo()
represent current class method implicitly as this.method(), current class object as this.obj,
pass an argument in the method call, pass an argument in the constructor call and so on
this refer current class instance variable
example1:
class javaClass
{
int x;
String s;
// Constructor with a parameter
javaClass(String s, int x)
{
this.x = x;
this.s= s //this can be also used as x=x or s=s
}
// Call the constructor
public static void main(String[] args)
{
javaClass Obj = new javaClass (“sony”, 25);
System.out.println("Value of x = " + Obj.x);
}
}
Example 2:
public class this1
{
int i;
void show(int i)
{
this.i=i;
this.i++;
System.out.println(i);
}
void go()
{
System.out.println(i);
}
class demo
{
public static void main(String a[])
{
this1 t=new this1();
t.show(44);
t.go();
}
}