IT oop ch-3
IT oop ch-3
Everything (data and methods) in Java is contained in classes. So far you've been using classes to hold
methods (main and perhaps a few other static methods). These methods use local variables, which are
completely private to the method, and which disappear when the method returns.
Real-time example 1:
Let us consider two objects Samsung Galaxy S4 and iPhone. Suppose Samsung Galaxy S4 have some
properties like width="6.98 cm", height="13.6 cm", OS="Android", brand="Samsung", price="1000$"
and Actions are call(), sendMessage(), browser(), share().
Now, suppose iPhone has some properties such as width="5.86 cm", height="12.3 cm", OS="iOS",
brand="Apple", price="1200$" and actions are call(), sendMessage(), browse(), share().
Both objects have some different properties and actions but the type is the same "Phone". This is the
class. i.e the name of the class is "Phone".
Real-time example 2:
Consider two objects one boy and one girl. The boy has some properties like hairColor="black",
eyeColor="black", skinColor="Fair", height="5.10 inch", weight="65 kg" and actions are read(), play(),
sleep(), walk().
Now, The girl has some properties like hairColor="Brown", eyeColor="brown", skinColor=" white",
height="5.4 inch", weight="50 kg" and actions are read(), play(), sleep(), walk(). but the type of both is
the same. i,e Person. So the class name is 'Person'.
A class definition is a template for creating objects. A class defines which fields an object has in it. We need to use
new to create a new object from the class by allocating memory and assigning a default value to each field.
The constructor Employee () is a default or a non-parameterized constructor, because it doesn’t use parameters.
Default parameter is used to initialize the object with default value.
Parameterized Constructor has parameter list to receive arguments for populating the instance attributes. When we
create a new instance (a new object) of a class using the new keyword, a constructor for that class is called.
Constructors are used to initialize the instance variables (fields) of an object and constructors are similar to methods,
but with some important differences.
➢ Constructor name is class name i.e., they must have the same name as the class.
➢ Default constructor. If you don't define a constructor for a class, a default (parameterless) constructor is
automatically created by the compiler. The default constructor calls the default parent constructor (super ( ))
and initializes all instance variables to default value (zero for numeric types, null for object references, and false
for Booleans).
Differences between methods and constructors
• There is no return type given in a constructor signature (header). The value is this object itself so there is no
need to indicate a return value.
• There is no return statement in the body of the constructor. There can be more than one constructor in a class,
distinguished by their parameters. When the class is initialized, Java will call the right constructor with matching
arguments and type.
Example 1: //without constructors
public class Circle {
public int x, y; // centre of the circle
public int r; // radius of circle
//Methods to return circumference and area
public double circumference()
{ return 2*3.14*r; }
public double area()
{ return 3.14 * r * r; }
public static void main(String args[]) {
Circle C; // creating reference
C = new Circle(); // creating object
C.x = 10; // assigning value to data field