Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Programming-Unit II

Uploaded by

kesavanram07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Programming-Unit II

Uploaded by

kesavanram07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT II

I. CLASSES, OBJECTS & METHODS


Program
import java.io.*;
class room
{
double length;
double breadth;
room(double x, double y)
{
CLASS- ROOM
length=x;
OBJECT1-ROOM1 OBJECT2-ROOM2
breadth=y;
} Length-25.0 Length-20.0

room(double x) Breadth-15.0 Breadth-20.0


{
Area ( )-375.0 Area ( )-400.0
length=breadth=x;
}
double area()
{
return(length*breadth);
}
}
class methodoverload
{
public static void main(String args[])
{
room room1=new room(25.0,15.0);
System.out.println("\nLength=25.0\nBreadth=15.0\n");
double rect=room1.area();
System.out.println("Area of Rectangle="+rect);
room room2=new room(20.0);
System.out.println("\nLength=Breadth=20.0\n");
double square=room2.area();
System.out.println("Area of Square="+square);
}
}

Defining Class
 A class is a user-defined data type with a template.
 Class definition:
class class-name [extends super-class-name]
{
[Fields declaration;]
[Methods declaration;]
}
 Everything inside square brackets – optional.
 class empty
{
} \\ Do nothing class
 class-name & super-class-name – valid Java identifiers.
 Keyword „extends‟ – super class inherits the properties of class name.

Fields Declaration
 Data fields (called, instance variables), are placed inside the body of class definition.
Example: double length;
double breadth;
 They are instance variables, because they are created whenever an object of the class is
instantiated.

Methods Declaration
 General Form:
type method-name (parameter list)
{
method-body;
}
 Example:
room(double x, double y)
{
length=x;
breadth=y;
}
 Four Parts:
1. Method Name – valid identifier
Example: room ( )
2. Method Type – return type of method. It could be void or data type.
3. Parameter list – list of variable names and types of all input values separated by commas.

Creating Objects
 General Form:
class-name object-name = new class-name ( )
 Creating an object referred as instantiating an object.
 Objects are created using „new‟ operator.
 The „new‟ operator creates an object of the specified class and returns a reference to object.
Example: room room1=new room ( );

Accessing Class Members


 General Form:

object-name. variable-name = value;

object-name. method-name(parameter-list);

 Class members are accessed using the concerned object and dot operator.
 Object name – name of the object.
 variable name – instance variable we wish to access.
 method-name – method we wish to call.
 parameter-list – actual values separated by comma. They must match in number and type of
method-name declared in class.
 Example:

room1.area();

room1.length = 15;

II. CONSTRUCTORS
 Constructors, special type of method that enables an object to initialize itself when it I created.
 Constructors have the same name as the class itself.
 They do not specify return type, not even void.
 Parameterized Constructor – at the time of object instantiation, the constructor is explicitly
invoked by passing arguments.
Example:
room(double x, double y)
{
length=x;
breadth=y;
}
 Default Constructor – does not take any parametric values.
Example:
room ( )
{
}

III. METHOD OVERLOADING


 Method Overloading: Methods that have the same name, but different parameters lists and
different definitions. The difference may either be in the number or type of arguments.
 Polymorphism: When a method in an object is called, Java matches up the method name first
and then the number and type of parameters to decide which one of the definitions to execute.
 Example:
class room
{
double length;
double breadth;
room(double x, double y)
{
length=x;
breadth=y;
}
room(double x)
{
length=breadth=x;
}
double area()
{
return(length*breadth);
}
}

IV. STATIC MEMBERS


 To define a member that is common to all the objects and accessed without using a particular
object, the members are declared static and called static members.
 Static members belong to the class as whole rather than the objects created from the class.
 Static variables are used when we want to have a variable common to all instances of a class.
 Static methods are called using class names.
 Restrictions:
- They can only call other static methods.
- They can only access static data.
- They cannot refer to „this‟ or „super‟ in any way.
Program:
class operation
{
static float mul(float x, float y)
{
return x * y;
}
static float divide(float x, float y)
{
return x / y;
}
}
class application
{
public static void main(String args[ ])
{

float a = operation.mul(4.0, 5.0);

float b = operation.divide(a, 2.0);


System.out.println(“B=”+b);
}
}

Output: B = 10

V. INHERITANCE: EXTENDING A CLASS


 Inheritance: The mechanism of deriving a new class from an old one is called inheritance.
 Old class: base class / super class / parent class.
 New class: sub class / derived class / child class.
 Inheritance allows subclasses to inherit all the variables and methods of their parent class.
 Different forms of inheritance:
1. Single inheritance – only one super class. A
class A \\ base class
{…}
B
class B extends A \\ derived class
{…}

2. Multilevel inheritance- derived from a derived class.


A
class A \\ base class
{…}
class B extends A \\ first level derived class B
{…}
class C extends B \\ second level derived class
C
{…}
The class A serves as a base class for the derived class B which in turn serves as a base
class for the derived class C.
A

3. Hierarchical inheritance – one super class, many subclasses.


 Defining a subclass B C D
General Form:
class sub-class-name extends super-class-name
{
variable declaration;
method declaration;
}
 The keyword „extends‟ signifies that the properties of the super-class are extended to sub-
class.
 The sub-class will now contain its own variables and methods as well as those of super-
class.
 Sub class constructor
 A sub class constructor is used to construct the instance variables of both sub class and
super class.
 The subclass constructor uses the keyword „Super‟ to invoke the constructor method of the
super class.
 Conditions for „Super‟ keyword:
1. „Super‟ may be used within a sub class constructor method.
2. The call to the super class constructor must appear.
3. The parameters in the super call must match the order and type of the instance variable
declared in the super class.

XI. OVERRIDING METHODS


Method Overriding: If the same method is defined in both the super class and the sub class, then
the method of the sub class overrides the method of the super class. This is known as method
overriding. Method overriding is used for runtime polymorphism.
 The method must have the same name as in the parent class.
 The method must have the same parameter as in the parent class.
 There must be inheritance.
Example:
import java.io.*;
class animal
{
public void display()
{
System.out.println("I am an animal");
}
}
class dog extends animal
{
public void display()
{
System.out.println("I am a dog");
}
}
class sample
{
public static void main(String args[])
{
dog d1=new dog();
d1.display();
}
}

You might also like