0% found this document useful (0 votes)
193 views

Unit 2 Constructors

this document is about to clear the concept of constructors and it is the part of the bsc it syllabus

Uploaded by

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

Unit 2 Constructors

this document is about to clear the concept of constructors and it is the part of the bsc it syllabus

Uploaded by

Kunal Kolhe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit II : Constructors

-- Constructor in java is a special type of method that is used


to initialize the object.
-- Java constructor is invoked at the time of object creation.
-- It constructs the values i.e. provides data for the object that is
why it is known as constructor.

Characteristics of Java Constructors


 An interface cannot have the constructor.
 Constructors cannot be private.
 A constructor cannot be abstract, static, final, native or
synchronized
 A constructor can be overloaded.
 Constructors cannot return a value.
 Constructors do not have a return type; not even void.
 An abstract class can have the constructor.
 Constructors name must be similar to that of the class
name inside which it resides.
 Constructors are automatically called when an object is
created.

Rules for creating Java constructor


1. Constructor name must be same as its class name
2. Constructor must have no explicit return type

Types of Java constructors


There are two types of constructors:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
1. Default Constructor
- No parameter
Syntax :
<class_name>()
{ }

Example
Class Bike1
{
Bike1()
{
System.out.println("Bike is created");
}

public static void main(String args[])


{
Bike1 b=new Bike1();
}
}

Note: If there is no constructor in a class, compiler


automatically creates a default constructor.

-- Default constructor provides the default values to the object


like 0, null etc. depending on the type.
Example of default constructor
Class Student3
{
int id;
String name;
void display()
{
System.out.println(id+""+name);
}
public static void main(String args[])
{
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display(); s2.display();
}
}

2. Parameterized Constructor :
-- A constructor with parameters.
-- If we want to initialize fields of the class with your own
values, then use parameterized constructor.

Class Student
{
String name;
Student(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+""+name);}
public static void main(String args[])
{
Student s1 = new Student4(111,"Karan");
Student s2 = new Student4(222,"Aryan");
s1.display(); s2.display();
}
}

this keyword in java


--In java, this is a reference variable that refers to the current
object.
-- programmers can refer to any member of the current object
within an instance method or a constructor.

Usage of Java this keyword


1. To refer current class instance variable.
2. To invoke current class method (implicitly)
3. To invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from
the method.
Example: to refer current class instance variable
class Emp
{
int e_id;
String name;
Emp(int e_id, String Emp(int id, String n)
name) {
{ e_id = id;
this.e_id = e_id; name = n;
this.name = name; }
}
void show()
{
System.out.println(e_id + ""
+ name);
}
public static void main(String
args[])
{
Emp e1 = new Emp(1006,
"Karlos");
Emp e2 = new Emp(1008, "Ray");
e1.show(); e2.show();
}
}

Example: to invoke current class method


Class A
{
void m()
{
System.out.println("hello m");
}
void n()
{
System.out.println("hello n");
this.m();
}
}
class Test
{
public static void main(String
args[])
{
A a=new A();
a.n();
}
}
O/P: hello n
hello m

Example: to invoke current class constructor


Class A
{
A()
{
System.out.println("hello a");
}
A(int x)
{
this();
System.out.println(x);
}
}
class Test
{
public static void main(String args[])
{
A a=new A(10);
}
}
O/P: hello a
10

Example: Real usage of this()


constructor call

class Student
{
int rollno;
String name,course;
float fee;

Student(int rollno,String name,String


course)
{
this.rollno=rollno;
this.name=name;
this.course=course;
}
Student(int rollno,String name,String
course,float fee)
{
this(rollno,name,course);
//reusing constructor
this.fee=fee;
}
void display()
{

System.out.println(rollno+""+name+""+co
urse+""+fee);
}
}
class Test
{
public static void main(String args[])
{
Student s1=new
Student(111,"ankit","java");
Student s2=new
Student(112,"sumit","java",6000);
s1.display();
s2.display();
}
}
super keyword in java
-- It is a reference variable which is
used to refer immediate parent class
object.
-- Whenever you create the instance of
subclass, an instance of parent class is
created implicitly which is referred by
super reference variable.

Usage of java super Keyword


1. to refer immediate parent class
instance variable.
2. to invoke immediate parent class
method.
3. to invoke immediate parent class
constructor.

1) super is used to refer immediate parent class instance


variable.
We can use super keyword to access the
data member or field of parent class. It
is used if parent class and child class
have same fields.
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
System.out.println(color);
System.out.println(super.color);
}
}
class Test
{
public static void main(String args[])
{
Dog d=new Dog();
d.printColor();
}
}

2) super can be used to invoke parent class method


The super keyword can also be used to
invoke parent class method. It should be
used if subclass contains the same method
as parent class. In other words, it is
used if method is overridden.
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("eating bread...
");
}
void bark()
{
System.out.println("barking...");
}
void work()
{
super.eat();
bark();
}
}
class Test
{
public static void main(String args[])
{
Dog d=new Dog();
d.work();
}
}

3) super is used to invoke parent class constructor.


The super keyword can also be used to
invoke the parent class constructor. Let's
see a simple example:
class Animal
{
Animal()
{
System.out.println("animal is created")
;
}
}
class Dog extends Animal
{
Dog()
{
super();
System.out.println("dog is created"
);
}
}
Class Test
{
public static void main(String args[])
{
Dog d=new Dog();
}
}

------------------------------------------------------------------------------------------------------
-----

Constant in Java
-- A constant is a variable which cannot
have its value changed after declaration.
It uses the 'final' keyword.

Syntax:
# Global constant
modifier final dataType variableName =
value;

# Constant within a class


modifier static final dataType
variableName = value;
e.g.
private static final double PI = 3.1415
9;

------------------------------------------------------------------------------------------------------
---------
static fields(variables) of a class

-- A static field belongs to the class.


-- Thus, no matter how many objects you
create of that class, there will only
exist one field located in the class, and
the value of that field is the same, no
matter from which object it is accessed.

-- Static Java fields are located in the class,


not in the instances of the class.
-- You define a static field by using the
static keyword in the field declaration:
public class Customer
{
static String str1;
}

-- Static fields are located in the class,


so you don't need an instance of the class
to access static fields. You just write
the class name in front:
Customer.str1 = "abc";
System.out.println(Customer.str1);

------------------------------------------
----------------------------------------

Java static method

-- It belongs to the class rather than


object of a class.
-- No need to create an instance of a
class to access it.
-- can access static data member and can
change the value of it.

class Student
{
static String college = "ITS";

static void change()


{
college = "SJCHS";
}

void display ()
{
System.out.println(college);
}

public static void main(String args[])


{
Student.change(); //static method
call without class instance
Student s1 = new Student();
s1.display();
}
}

O/P: SJCHS
Restrictions for static method

1. The static method cannot use non static data


member or call non-static method directly.
2. this and super cannot be used in static
context.

class A
{
int a=40; //non static
public static void main(String args[])
{

System.out.println(a);
}
}

O/P: Compile Time Error

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy