Java program please Answer the following question What is en
Java program please!
Answer the following question What is encapsulation? Why is it useful? What is an instance method? How is it different from a static method? What is a constructor? How many constructors a class can have? Can a class have no constructors? If a class has more than one constructors, which of them get called? What is the difference between the number 0, the null reference, the value false, and the empty string? Design and implement a class Country that stores the name of the country, its population, and its area. Then write a program that reads in a set of countries and prints The country with the largest area. The country with the largest population. The country with the largest population density (people per square kilometer (or mile)).Solution
a.What is encapsulation?Why is it useful?
Answer:
Definition of Encapsulation: Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.
if the field is declared private,it cannot be accessed by anyone outside the class,there by hiding the fields within the class.
It is useful to allows the programmer to group data and methods that operate on them together in one place and to hide details from the user
in java, hiding details is done by making them private
Eg:
class BankAccount
{
private double accountBalance;
public withdraw();
public deposit();
public determineBalance();
}
b.What is an instance method? How it is different from static method?
Definition of instance method :Methods defined in class which is only accessed through object of a class
static methods useful when accessing static variables
Eg:public static int getCount()
Static methods cannot access instance variables or methods.But, Instance methods can access static & instance variables & methods
c.What is a constructor?How many constructors a class can have? can a class have no constructors?if a class has more then one constructors,which on them get called?
Answer: Definition of constructor:
There are basically two rules defined for the constructor:
constructor name must be same name as classs name
constructor must have no explicit return type
A class can have multiple constructors, but they must have different parameter lists
Of course ,can a class have no constructors.these constructors are defined based on requirement.
if a class has more then one constructors, then constructor get called which is includes all other constructor.
import java.io.*;
class A
{
public int value1, value2;
public A
{
value1=20;
value2=30;
}
public void display(int value1,int value2)
{
System.out.println(\" the value1=\"+value1);
System.out.println(\" the value2=\"+value2);
}
}
class B extends A
{
public int value1,value2;
public B
{
}
}
class Cons
{
public static void main(String args[])
{
System.out.println(\" Start of main()\");
B b= new B;
b.display(2,10);
}
}
Here in the above example the constructor B get called instead of Constructor A.
d.What is the difference between the number 0,the null reference,the value false, and the empty String?
Answer:
0 is a numeric data type with a value of 0. null reference means that a variable contains a reference to a space in memory that does not contain an object.

