UNIT 2 Java
UNIT 2 Java
Semester- II
Unit 2
objectname.variablename
Example:
japan.population = 58612453;
bhutan.currancyRate = 0.236;
japan.noOfSates = 5;
Adding methods
The general form of the declaration of the method is,
} return(cal);
int add(int z) //version2
{
cal = z + 10;
return(cal);
}
float add(float x, float y) //version3
{
val = x + y;
return(val);
}
Static members
●
Methods and variables defined inside the class
are called as instance methods and instance
variables. That is, a separate copy of them is
created upon creation of each new object.
●
But in some cases it is necessary to define a
member that is common to all the objects and
accessed without using a particular object.
●
That is, the member belongs to the class as a
whole rather than the objects created from the
class. Such members can be created by
preceding them with the keyword static.
Example:
static int cal;
static float min = 1;
static void
display(int x)
Static members
●
When a member is declared static, it can be accessed
before any objects of its class are created, and without
reference to any object.
● We can declare both methods and variables to be
● static.
The most common example of a static member is
main( ). main( ) is declared as static because it must be
● called before any objects exist.
Instance variables declared as static are, essentially,
global variables.
Restrictions to static
● They can only call other static methods.
● They must only access static data.
● They cannot refer to this or super in
any way.
Argument passing to methods
● Call by value
● Call by reference
Call by value
class Test
{
void meth(int i, int j)
{
i++;
j+
} +;
}
ob.meth(a, b);
Call by reference
class Test
{
int a, b;
void meth(Test o)
{
a. ++;
b. ++;
}
}
Ob1.meth(obj2);
Call by Value means calling a method with a
parameter as value. Through this, the argument
value is passed to the parameter.
While Call by Reference means calling a method
with a parameter as a reference. Through this,
the argument reference is passed to the
parameter.
In call by value, the modification done to the
parameter passed does not reflect in the caller's
scope while in the call by reference, the
modification done to the parameter passed are
persistent and changes are reflected in the
caller's scope.
Command line arguments
public static void main(String args[ ])
javac prog.java
java prog Hello my name is Tushar
args[0] = Hello
args[1] = my
args[2] = name
args[3] = is
args[4] = Tushar
The Object class
● There is one special class, ‘Object’, defined by Java.
●
All other classes are subclasses of ‘Object’. That is,
‘Object’ is a super class of all other classes.
●
This means that a reference variable of type ‘Object’
can refer to an object of any other class.
●
Also, since arrays are implemented as classes, a
variable of type ‘Object’ can also refer to any array.
The object class
Class visibility
controls
● Public
● Private
● Protected
● Default
Public access
●
If we declare any variable and method as
‘public’, it will be accessible to all the entire
class and visible to all the classes outside the
class. It can be accessed from outside package
also. For example,
●
The default access can also be called as package
access or friendly access.
● When no member accessibility modifier is
specified, the member is only accessible inside its
own package only.
●
Even if its class is visible in another package, the
member is not accessible there.
●
Default member accessibility is more restrictive
than the protected member accessibility.
Default access
Private access
● This is most restrictive than all other visibility controls.
● Private members are not accessible from any other class.
● They can not be accessed even from any subclass or
any class from the same package. In order to declare a
member as private, we just need to write ‘private’ in front
of the member as,
●
It will create the instance of the string with no characters in it. If we want to
initialize the string with values we can use following constructor.
String(char
chars[])
●
For example:
String s = “Pramod”;
int len = s.length();
String concat
String s = “First”;
String t = “Second”;
s.concat(t);
Character at
String s = “Indian”;
char ch = s.charAt(2);
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
How to use StringBuffer?
StringBuffer sb = new StringBuffer("Oriya");
Vector()
Vector(int size)
size, int incr)
Vector(int
Operations of Vector
));
v.addElement(new
Integer(23));
v.addElement(new Float(9.6f));
Wrapper classes
●
The primitive data types of Java such as int, char,
float are not the part of any object hierarchy.
●
They are always passed by value to the method not
by reference.
●
Object representations of primitive data types are
called as wrapper classes.
●
In essence, these classes encapsulate or wrap the
primitive data types within the class. All the
wrapper classes are defined in package java.lang.
Wrapper classes
Data type Wrapper class
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short
byte Byte
Example:
Character ch = new Character('X');
System.out.print("Char value: ");
System.out.println(ch.charValue());
if(Character.isDigit('0'))
System.out.println("0 is digit");
if(Character.isLowerCase('R'))
System.out.println("R is lower case");
Enumerated data type
●
Enum is type like class and interface and can
be used to define a set of Enum constants.
● Enum constants are implicitly static and final
and you can not change there value once
created.
● Enum in Java provides type-safety and can be
used inside switch statment like int variables.
Example:
super.variablename;
super(argumentlist);
● The argument list specifies any arguments needed by constructor in the super class.
Remember ‘super ( )’ must always be the first statement executed inside a subclass’
constructor.
Method overriding
● In a class hierarchy, when a method in a subclass has
the same name, same arguments and same return
type as a method in its super class, then the method
in the subclass is said to override the method in the
super class.
● When an overridden method is called from within a
subclass, it will always refer to the version of that
method defined by the subclass.
● The version of the method defined by the super class
will be hidden.
Example:
class Maths
{
int var1, var2, var3;
Maths(int x, int y)
{
var1 = x;
var2 =
y;
}
v oid calculate() //statement1
{
var3 = var1 + var2;
System.out.println("Addition : "+var3);
}
}
class Arithmetic extends Maths
{
Arithmetic(int x,int y)
{
super(x,y);
}
void calculate() //statement2
{
var3 = var1 - var2;
System.out.println("Subtraction : "+var3);
}
}
Dynamic method dispatch
● Dynamic method dispatch is the mechanism by which a
call to an overridden method is resolved at run time, rather
than compile time.
● Dynamic method dispatch is important because this is the
only way in which Java implements run-time
polymorphism.
● If a super class contains a method that is overridden by a
subclass, then when different types of objects are referred
to through super class reference variable, different
versions of the method are executed.
● This concept is C++ counterpart of Virtual function in
Java.
Example:
class Principal {
void message()
{ System.out.println("This Principal");
} is
}
class Professor extends Principal {
void message()
{ System.out.println("This Professor");
} is
}
class Lecturer extends Professor {
void message()
{ System.out.println("This Lecturer");
} is
}
Example continued...
class Dynamic {
public static void main(String args[]) {
Principal x = new Principal();
Professor y = new Professor();
Lecturer z = new Lecturer();
Principal ref; //reference variable of super class
ref = x;
//statement1
ref.message();
ref = y;
//statement2
ref.message();
ref = z;
//statement3
ref.message();
Abstract classes and methods
●
Sometimes we will want to create a super class
that only defines a generalized form that will be
shared by all of its subclasses by leaving it to
each subclass to fill in the details.
●
Such a class determines the nature of the
methods that the subclasses must implement.
This can be done by creating a class as an
abstract.
Abstract
●
We can indicate that a method must always be
redefined in a subclass by making the
overriding compulsory.
● This can be achieved by preceding the keyword
‘abstract’ in the method definition with following
form:
●
We can not create the objects of an abstract class. That is, an
abstract class cannot be directly instantiated with the new operator.
Application of 'final' in inheritance
● Prevent the method overriding
● Prevent the inheritance
Prevent method overriding
class First {
final void display()
{ System.out.println(“This is first
class”);
}
class Second extends First {
void display()
{
System.out.println(“This is second class”);
}
}
error
Prevent inheritance
final class First
{
// ….
}
class Second extends First
{
// ….
}
error
References
Thank you