Abstraction: Abstraction Is A Process Where You Show Only "Relevant" Data and "Hide"
Abstraction: Abstraction Is A Process Where You Show Only "Relevant" Data and "Hide"
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
Polymorphism
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
Example of static polymorphism where binding of method call to its definition at Compile
time.
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
}
}
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
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
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();
}
}