0% found this document useful (0 votes)
23 views2 pages

Understanding Methods and Constructors

The document discusses methods and constructors in programming, defining methods as blocks of statements that perform specific tasks and categorizing them based on return types and arguments. It explains constructors as special methods for initializing objects, with two types: default and parameterized constructors. Additionally, it includes example code for classes demonstrating the use of constructors and methods, along with potential interview questions related to the topic.

Uploaded by

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

Understanding Methods and Constructors

The document discusses methods and constructors in programming, defining methods as blocks of statements that perform specific tasks and categorizing them based on return types and arguments. It explains constructors as special methods for initializing objects, with two types: default and parameterized constructors. Additionally, it includes example code for classes demonstrating the use of constructors and methods, along with potential interview questions related to the topic.

Uploaded by

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

Revision : Methods & Its Types

Methods
--> Block of statements which perform a specific task

Types : -- with no returntype & no args


-- with no returntype & with args
-- with returntype & no args
-- with returntype & with args

Agenda : Constructors

Special method which is used to initialize the object

Types of cons : 2 types :


a] Default cons
b] Param cons

package basicExamples;

public class Student {

String name;
int rollNo;

// Student()
// {
// name = null;
// rollNo=0;
//
// }

//with two paramterised


public Student(String n,int i)
{
name = n;
rollNo = i;
}

// Method
public void displayData() {
[Link]("Name: " + name + " " + "RollNo: " + rollNo);
}

package basicExamples;

public class MyMainClass {

public static void main(String[] args) {


//constructor call
//ClassName objectName = new ClassName();
Student s1 = new Student("Priya",1); // s1--> name rollno
[Link]();

Student s2 = new Student("Shreya",2); // s2--> name rollno


[Link]();

Student s3 = new Student("Neha",3); // s3--> name rollno


[Link]();
//
// Student s4 = new Student();
// [Link]();
//

}
}

package basicExamples;

public class Demo {

public Demo() {

[Link]("Zero-param cons.");
}

public Demo(int i) {

[Link]("One-param cons."+i);
}

public Demo(int i,int j) {

[Link]("two-param cons."+i+" "+j);


}
public static void main(String[] args) {

Demo ob1 = new Demo();


Demo ob2 = new Demo(10);
Demo ob3 = new Demo(12, 10);
}

Interview Questions :

1] what are constructors ?


2] What are different types of cons ?
3] What are default const.?
4] what is the diff between cons and method ?

You might also like