java unit 2
java unit 2
• Classes
• Objects
• Constructors
• Overloading method
• Access control
• Static and fixed methods
• Inner classes, string classes
• Inheritance, over riding method
• Abstract class
CLASS
2
syntax of CLASS
class <ClassName>
{
attributes/variables;
Constructors();
methods();
}
3
INSTANCE
• Creating an Instance
ClassName refVariable;
refVariable = new Constructor();
or
ClassName refVariable = new Constructor();
4
Methods
5
BYTE CODE
6
Helloworld
• To begin you need a text editor.
– Notepad, TextPad, …
– Create a new directory.
• Open new file, save it as HelloWorld.java in new directory
• Write the following code and save the file:
/*simple java application*/
class HelloWorld
{
public static void main(String[] args)
{
System.out.println(“Hello World”); //displays a string
}
}
• Note: the file name should have the same name as the class
7
Compiling and running
8
Closer Look
10
Modifiers
Java uses certain reserved words called modifiers that specify the
properties of the data, methods, and classes and how they can be
used. Examples of modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public datum, method, or class
can be accessed by other programs. A private datum or method cannot
be accessed by other programs.
11
Statements
13
Closer Look, cont…
• This is the string array that will contains the command line
arrguments.
– The main method is the entry point for your application and will
subsequently invoke all the other methods required by your program.
• System.out.println("Hello World!");
– uses the System class from the API to print the "Hello World!" message
to standard output.
14
Class Definition
• A class contains a name, several variable declarations (instance variables) and several
method declarations. All are called members of the class.
• General form of a class:
class classname {
type instance-variable-1;
…
type instance-variable-n;
type method-name-1(parameter-list) { … }
type method-name-2(parameter-list) { … }
…
type method-name-m(parameter-list) { … }
}
15
Example: Class Usage
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println ("Volume is " + vol);
} }
16
Constructor
• A constructor initializes the instance variables of an object.
• It is called immediately after the object is created but before the
new operator completes.
1) it is syntactically similar to a method:
2) it has the same name as the name of its class
3) it is written without return type; the default
return type of a class
• constructor is the same class
• When the class has no constructor, the default constructor
automatically initializes all its instance variables with zero.
17
Example: Constructor
19
No – Arg Constructor
int empId;
String empName;
25
Access Control: Data Hiding and
Encapsulation
• Java provides control over the visibility of variables and
methods.
• Encapsulation, safely sealing data within the capsule of
the class Prevents programmers from relying on details
of class implementation, so you can update without
worry
• Helps in protecting against accidental or wrong usage.
• Keeps code elegant and clean (easier to maintain)
26
Access Modifiers: Public, Private, Protected
27
• Private fields or methods for a class only visible within that class.
Private members are not visible within subclasses, and are not
inherited.
• Protected members of a class are visible within the class, subclasses
and also within all classes that are in the same package as that class.
28
Visibility
public class Circle {
private double x,y,r;
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
29
Keyword this
30
Keyword this
• Keyword this allows a method to refer to the object that invoked it.
• It can be used inside any method to refer to the current object:
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
31
Garbage Collection
33
Method Overloading
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
void test(int a) {
System.out.println("a: " + a);
}
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
double test(double a) {
System.out.println("double a: " + a); return a*a;
}
}
35
Constructor Overloading
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
Box() {
width = -1; height = -1; depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() { return width * height * depth; }
}
36
Parameter Passing
37
Call by value
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.print("a and b before call: “);
System.out.println(a + " " + b);
ob.meth(a, b);
System.out.print("a and b after call: ");
System.out.println(a + " " + b);
}
}
38
Call by reference
• As the parameter hold the same address as the argument, changes to the
object inside the method do affect the object used by the argument:
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.print("ob.a and ob.b before call: “);
System.out.println(ob.a + " " + ob.b);
ob.meth(ob);
System.out.print("ob.a and ob.b after call: ");
System.out.println(ob.a + " " + ob.b);
}
}
39
Recursion
class Factorial {
int fact(int n) {
if (n==1) return 1;
return fact(n-1) * n;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.print("Factorial of 5 is ");
System.out.println(f.fact(5));
} } 41
Inner classes
• Java inner class or nested class is a class which is declared inside the
class or interface.
• We use inner classes to logically group classes and interfaces in one place
so that it can be more readable and maintainable.
• Additionally, it can access all the members of outer class including private
data members and methods.
• Syntax of Inner class
class Java_Outer_class
{
//code
class Java_Inner_class
{
//code
}
}
Advantage of java inner classes
• There are basically three advantages of inner classes in
java. They are as follows:
• 1) Nested classes represent a special type of
relationship that is it can access all the members
(data members and methods) of outer
class including private.
• 2) Nested classes are used to develop more readable
and maintainable code because it logically group
classes and interfaces in one place only.
• 3) Code Optimization: It requires less code to write.
class TestMemberOuter1
{
private int data=30;
class Inner
{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[])
{
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
String Handling
45
Empty Strings
• An empty String has no characters. It’s length is 0.
48
Methods — length, charAt
int length(); Returns the number of characters in
the string
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}}
50
Methods — substring
Returns a new String by copying characters from an existing String.