Vous êtes sur la page 1sur 6

Part two: Workout questions

1) Following are the difference between constructor and method.

 Constructor is used to initialize an object whereas method is used to exhibits functionality of an


object.

 Constructors are invoked implicitly whereas methods are invoked explicitly.

 Constructor does not return any value where the method may/may not return a value.

 In case constructor is not present, a default constructor is provided by java compiler. In the case
of a method, no default method is provided.

 Constructor should be of the same name as that of class. Method name should not be of the
same name as that of class.

2) Abstract Class
An abstract class is a class that is declared abstract — it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static
fields and static methods. When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class. However, if it does not, then the
subclass must also be declared abstract.

An abstract method is a method that is declared without an implementation (without braces and
followed by a semicolon)

3) Object − Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a
class.

New ClassName (parameters) // to create object


Eg Book b= new Book()

4) three types of access modifiers with examples by creating


code fragment of a class
1) Private
The private access modifier is accessible only within the class.

Simple example of private access modifier


In this example, we have created two classes A and Simple. A class contains private data
member and private method. We are accessing these private members from outside the
class, so there is a compile-time error.

1. class A{  
2. private int data=40;  
3. private void msg(){System.out.println("Hello java");}  
4. }  
5.   
6. public class Simple{  
7.  public static void main(String args[]){  
8.    A obj=new A();  
9.    System.out.println(obj.data);//Compile Time Error  
10.    obj.msg();//Compile Time Error  
11.    }  
12. }  
Role of Private Constructor

If you make any class constructor private, you cannot create the instance of that class from
outside the class. For example:

1. class A{  
2. private A(){}//private constructor  
3. void msg(){System.out.println("Hello java");}  
4. }  
5. public class Simple{  
6.  public static void main(String args[]){  
7.    A obj=new A();//Compile Time Error  
8.  }  
9. }  
Note: A class cannot be private or protected except nested class.

2) Protected
The protected access modifier is accessible within package and outside the package but
through inheritance only.

The protected access modifier can be applied on the data member, method and constructor.
It can't be applied on the class.

It provides more accessibility than the default modifer.


Example of protected access modifier

In this example, we have created the two packages pack and mypack. The A class of pack
package is public, so can be accessed from outside the package. But msg method of this
package is declared as protected, so it can be accessed from outside the class only through
inheritance.

1. //save by A.java  
2. package pack;  
3. public class A{  
4. protected void msg(){System.out.println("Hello");}  
5. }  
1. //save by B.java  
2. package mypack;  
3. import pack.*;  
4.   
5. class B extends A{  
6.   public static void main(String args[]){  
7.    B obj = new B();  
8.    obj.msg();  
9.   }  
10. }  
Output:Hello

3) Public
The public access modifier is accessible everywhere. It has the widest scope among all
other modifiers.

Example of public access modifier

1. //save by A.java  
2.   
3. package pack;  
4. public class A{  
5. public void msg(){System.out.println("Hello");}  
6. }  
1. //save by B.java  
2.   
3. package mypack;  
4. import pack.*;  
5.   
6. class B{  
7.   public static void main(String args[]){  
8.    A obj = new A();  
9.    obj.msg();  
10.   }  
11. }  
Output:Hello

5) Write java program that can guess the date of birth.

import java.util.Scanner;

public class GuessBirthday


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

String set1 =
" 1 3 5 7\n" +
" 9 11 13 15\n" +
" 17 19 21 23\n" +
" 25 27 29 31";

String set2 =
" 2 3 6 7\n" +
" 10 11 14 15\n" +
" 18 19 22 23\n" +
" 26 27 30 31";

String set3 =
" 4 5 6 7\n" +
" 12 13 14 15\n" +
" 20 21 22 23\n" +
" 28 29 30 31";

String set4 =
" 8 9 10 11\n" +
" 12 13 14 15\n" +
" 24 25 26 27\n" +
" 28 29 30 31";

String set5 =
" 16 17 18 19\n" +
" 20 21 22 23\n" +
" 24 25 26 27\n" +
" 28 29 30 31";

int day = 0;

// Create a scanner

Scanner input = new Scanner(System.in);

// Prompt the user to answer questions

System.out.print("Is your birthday in set1?\n");


System.out.print(set1);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
String answer = input.next();

if (answer.equals("Y"))
day += 1;

System.out.print("\nIs your birthday in set2?\n");


System.out.print(set2);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 2;

System.out.print("\nIs your birthday in set3?\n");


System.out.print(set3);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 4;

System.out.print("\nIs your birthday in set4?\n");


System.out.print(set4);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 8;

System.out.print("\nIs your birthday in set5?\n");


System.out.print(set5);
System.out.print("\nEnter (Y) for yes or (N) for no: ");
answer = input.next();

if (answer.equals("Y"))
day += 16;

System.out.println("\nYour birthday is " + day + "!");


}
}

Vous aimerez peut-être aussi