Part 8
36 : What is the difference between the Constructor and Method?
Answer : Main difference between the Constructor and Method is as bellow.
Constructor :
Answer : Main difference between the Constructor and Method is as bellow.
Constructor :
- Name of the constructor must be same as class name.
- Constructor must not have any return type.
- It is used to initialize the state of an object.
- It is not possible to call constructor directly. Constructors called implicitly when the new keyword creates an object.
- Method name can be any.
- Method must have return type.
- It is used to expose behavior of an object.
- Methods can be called directly.
37 : What are the different types of types of constructors in java?
Answer : Mainly there are two types of constructors available in java.
1 New York
2 London
38 : Write a program for Fibonacci series in Java ?
Answer : Program for Fibonacci series is as bellow.
39 : Write a program to print below given pattern.
Answer : Program to print above pattern is as bellow.
40. What is the difference between “this” and “super” keywords in Java?
Answer : Difference is as bellow.
Answer : Mainly there are two types of constructors available in java.
- Default Constructor : Constructor without parameter is called default constructor.
package JAVAExamples;
public class City {
//Default Constructor
City()
{
System.out.println("City is created");
}
public static void main(String args[]){
City c=new City();
}
}
- Parameterized constructor : Constructor with parameter is called Parameterized constructor.
package JAVAExamples;
public class City {
int id;
String name;
// parameterized Constructor
City(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
City c1 = new City(1, "New York");
City c2 = new City(2, "London");
c1.display();
c2.display();
}
}
Output :1 New York
2 London
38 : Write a program for Fibonacci series in Java ?
Answer : Program for Fibonacci series is as bellow.
package JAVAExamples;
public class FibonacciSeries {
public static void main(String args[]) {
int x1 = 0, x2 = 1, x3, i, cnt = 15;
// To print 0 and 1
System.out.print(x1 + " " + x2);
// loop starts from 2 as 0 and 1 are already printed.
for (i = 2; i < cnt; ++i) {
x3 = x1 + x2;
System.out.print(" " + x3);
x1 = x2;
x2 = x3;
}
}
}
39 : Write a program to print below given pattern.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answer : Program to print above pattern is as bellow.
package JAVAExamples;
public class Pattern {
public static void main(String[] args) {
for (int a = 1; a <= 5; a++) {
for (int x = 1; x <= a; x++) {
System.out.print(x+" ");
}
// To print new line.
System.out.println();
}
}
}
40. What is the difference between “this” and “super” keywords in Java?
Answer : Difference is as bellow.
- “this” keyword is used to store current object reference while “super” keyword is used to store super class object in sub class..
- “this” is used to access methods of the current class while “super” is used to access methods of the base class.
- this() used to call constructors in the same class whereas super() is used to call super class constructor.
Read more on “super” keyword.
No comments:
Post a Comment