0% found this document useful (0 votes)
42 views7 pages

Constructor

The document provides an introduction to constructors in object-oriented programming, explaining their role in creating class objects and accessing class variables and methods. It highlights that constructors must have the same name as the class and can be overloaded with multiple versions. Examples are provided to illustrate the use of both default and parameterized constructors in a Student class.
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)
42 views7 pages

Constructor

The document provides an introduction to constructors in object-oriented programming, explaining their role in creating class objects and accessing class variables and methods. It highlights that constructors must have the same name as the class and can be overloaded with multiple versions. Examples are provided to illustrate the use of both default and parameterized constructors in a Student class.
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
You are on page 1/ 7

CSC 302

(Introduction to OOP)

Constructors
Course Instructor: Mr. Umar Ibrahim
Federal University Dutse
Adopted from oracle java documentation: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
What is a constructor ?
A Constructor provides a way in which classes can communicate. A constructor creates an object of a class and is a way in which we can
talk(access) variables and methods of a class.

NB: The name of a constructor must be the same with the class name and they do not have a return type. Understanding constructors is
super important.

We can have more than one constructor in a class. If we do not create any constructor, a default constructor is provided for us.

Examples of using constructors

E.g 1.

Class Student {

//This constructor has no parameters and it is provided even if we do not explicitly provide it.

Public Student(){

The above is a class called Student that has one constructor with no parameters.

We can create objects using the constructor like below:

Student s1 = new Student(); or Student studentObject= new Student() //the name can be anything like s1,s2,studentObject,student1 or
anything
Let’s play with classes and constructors

We basically have
created 3 objects of
Itstudents class

and

3 objects from
Cscstudents class.
I.e we used a
constructor
The previous classes used only the default constructors. Let’s make use of
parameterized constructors i.e constructors with variables inside this ()
A student class with 4 variables
Constructor 1(No parameter) (age,name,email and maritalStatus)
Constructor 2(1 parameter)
Four constructors and

Constructor 3(2 parameters)


Two methods
getName() and getAge()

Constructor 4( 4 parameters)
Program output
Thank you

You might also like