0% found this document useful (0 votes)
3 views13 pages

9 Java Constructors

Uploaded by

anovalexter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

9 Java Constructors

Uploaded by

anovalexter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA CONSTRUCTORS

JAVA CONSTRUCTORS
A constructor in Java is a special method that is used to initialize
objects. The constructor is called when an object of a class is created. It
can be used to set initial values for object attributes:
• Note that the constructor name must match the class name, and it
cannot have a return type (like void).
• Also note that the constructor is called when the object is created.
• All classes have constructors by default: if you do not create a class
constructor yourself, Java creates one for you. However, then you are
not able to set initial values for object attributes.
TYPES OF JAVA CONSTRUCTORS
• Default constructor (no-arg constructor) - (or no-arg constructor) is a
constructor that takes no parameters and is automatically provided
by the Java compiler if no other constructor is explicitly defined in a
class. Its primary purpose is to initialize objects with default values
(\(null\) for objects, \(0\) for numbers, \(false\) for booleans).
• Parameterized constructor - a special type of method in object-
oriented programming (OOP) that accepts arguments (parameters) to
initialize an object with specific values at the moment it is created.
return(length * width); class Rectangle
} }
{
class RectArea()
{ int length,width;
public static void main(String args[]) Rectangle (int x, int y)
{ {
length=x;
Rectangle rect1=new
Rectangle(15,10); width=y;
int area1=[Link](); }
[Link](“Area1 =“ +
area1); int rectArea()
} {
}
STATIC MEMBERS:
• In Java, static members are those which belongs to the class and you
can access these members without instantiating the class.
• The static keyword can be used with methods, fields, classes
(inner/nested), blocks.
• Static Methods − You can create a static method by using the
keyword static. Static methods can access only static fields, methods.
To access static methods there is no need to instantiate the class, you
can do it just using the class name.
Advantages of static variable
• It makes your program memory efficient (i.e., it saves memory).
class MathOperation
{
static float mul(float x, float y)
{
return(x * y);
}
static float divide(float x, float y)
{
return(x /y);
}}
class MathApplication
{
public static void main(String args[])
{
float a=[Link](4.0,5.0);
float b=[Link](a,2.0);
[Link](“b=“ +b);
}
}
METHOD OVERLOADING AND METHOD
OVERRIDING IN JAVA:
Method Overloading is a Compile time polymorphism. In method
overloading, more than one method shares the same method name
with different signature in the class. In method overloading, return
type can or can not be same, but we must have to change the
parameter because in java, we can not achieve the method
overloading by changing only the return type of the method.
METHOD OVERLOADING AND METHOD
OVERRIDING IN JAVA:
Method Overriding is a Run time
polymorphism.
In method overriding, derived class provides the specific
implementation of the method that is already provided by the base
class or parent class. In method overriding, return type must be same.
Method Overriding is a Run
time polymorphism.
Method Overriding is a Run time
polymorphism.
• Here, we can see that a method eat() has overridden in the derived
class name Dog that is already provided by the base class
name Animal.
When we create the instance of class Dog and call the eat() method,
we see that only derived class eat() method run instead of base class
method eat() and When we create the instance of class Animal and
call the eat() method, we see that only base class eat() method run
instead of derived class method eat().
• So, its clear that in method overriding, method is bound to the
instances on the run time which is decided by the JVM. That’s why it
is called Run time polymorphism
MANIPULATION OF STRINGS

class StringManipulation
{
public static void main(String args[])
{
StingBuffer str=new StringBuffer(“object language”);

[Link](“original string:” +str);

[Link](“length of string :” + [Link]());


//accessing character in a string

for(int i=0; i<[Link](); i++)


{
int p=i+1;
[Link](“character at position” + p + “is” + [Link](i));

}
MANIPULATION OF STRINGS

class StringManipulation
{
public static void main(String args[])
{
StingBuffer str=new StringBuffer(“object String astring=new String([Link]());
language”); int pos=[Link](“ language”);
[Link](7,”oriented”);
[Link](“original string:” +str);
[Link](“modified string” + str);
[Link](“length of string :” + [Link]()); [Link](6,’-’);
//accessing character in a string [Link](“string now” + string);
[Link](“improve security”);
[Link](“Appended string” + str);
for(int i=0; i<[Link](); i++)
{ }
}
int p=i+1;
[Link](“character at position” + p + “is” +
[Link](i));

You might also like