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 ?