Part 8
36 : What is the difference betwixt the Constructor too Method?
Answer : Main deviation betwixt the Constructor too Method is equally bellow.
Constructor :
Answer : Main deviation betwixt the Constructor too Method is equally bellow.
Constructor :
- Name of the constructor must hold upwards same equally shape name.
- Constructor must non accept whatever furnish type.
- It is used to initialize the Earth of an object.
- It is non possible to telephone telephone constructor directly. Constructors called implicitly when the novel keyword creates an object.
- Method cite tin hold upwards any.
- Method must accept furnish type.
- It is used to discover demeanor of an object.
- Methods tin hold upwards called directly.
37 : What are the dissimilar types of types of constructors inwards java?
Answer : Mainly at that spot are ii types of constructors available inwards java.
1 New York
2 London
38 : Write a plan for Fibonacci serial inwards Java ?
Answer : Program for Fibonacci serial is equally bellow.
39 : Write a plan to impress below given pattern.
Answer : Program to impress to a higher house designing is equally bellow.
40. What is the deviation betwixt “this” too “super” keywords inwards Java?
Answer : Difference is equally bellow.
Answer : Mainly at that spot are ii types of constructors available inwards java.
- Default Constructor : Constructor without parameter is called default constructor.
package JAVAExamples; world shape City { //Default Constructor City() { System.out.println("City is created"); } world static void main(String args[]){ City c=new City(); } }
- Parameterized constructor : Constructor alongside parameter is called Parameterized constructor.
package JAVAExamples; world shape City { int id; String name; // parameterized Constructor City(int i, String n) { id = i; cite = n; } void display() { System.out.println(id + " " + name); } world static void main(String args[]) { City c1 = novel City(1, "New York"); City c2 = novel City(2, "London"); c1.display(); c2.display(); } }
Output :1 New York
2 London
38 : Write a plan for Fibonacci serial inwards Java ?
Answer : Program for Fibonacci serial is equally bellow.
package JAVAExamples; world shape FibonacciSeries { world static void main(String args[]) { int x1 = 0, x2 = 1, x3, i, cnt = 15; // To impress 0 too 1 System.out.print(x1 + " " + x2); // loop starts from 2 equally 0 too 1 are already printed. for (i = 2; i < cnt; ++i) { x3 = x1 + x2; System.out.print(" " + x3); x1 = x2; x2 = x3; } } }
39 : Write a plan to impress below given pattern.
1 1 2 1 2 iii 1 2 iii four 1 2 iii four 5
Answer : Program to impress to a higher house designing is equally bellow.
package JAVAExamples; world shape Pattern { world static void main(String[] args) { for (int a = 1; a <= 5; a++) { for (int x = 1; x <= a; x++) { System.out.print(x+" "); } // To impress novel line. System.out.println(); } } }
40. What is the deviation betwixt “this” too “super” keywords inwards Java?
Answer : Difference is equally bellow.
- “this” keyword is used to shop electrical current object reference while “super” keyword is used to store super shape object inwards sub class..
- “this” is used to access methods of the electrical current class while “super” is used to access methods of the base of operations class.
- this() used to telephone telephone constructors inwards the same shape whereas super() is used to telephone telephone super shape constructor.
Read more on “super” keyword.