Short Question: 1.what Are Instance Variable?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Short Question

1.What are instance Variable?

An instance variable is a variable defined in a class (i.e. a member variable), for which each
instantiated object of the class has a separate copy, or instance. An instance variable is
similar to a class variable.

Example of an instance variable:

class Taxes
{
int count;
/*...*/
}

2.How can a GUI component handle its own events?

By implementing the required event listener interface a gui component can handle it own
event.In order to keep a track of all events associated with it uses its own event listener.A
program catches and processes GUI events by subclassing GUI components and override
either action() or handleEvent() methods.

3.what is difference between method overriding and


overloading?

Overloading:

When two or more methods (functions) in the same Class have the same name but
different parameters is called method overloading.

Class myClass
{
int Func(int x)
{
}
int Func(string s)
{
}
int Func(int x,string s)
{
}
}
Here you can see the functions name are same but parameter type and number of
parameters are different.

Overriding:
When two or more methods (functions) have the exact same method name, return
type, number of parameters, and types of parameters as the method in the parent
class is called method Overriding.

class parentClass
{
public virtual void Disp(int i)
{
System.Console.WriteLine("parentClass Display" + i);
}
}
class childClass : parentClass
{
public override void Disp(int i)
{
System.Console.WriteLine("childClass Display" + i);
}
}
Here you can see the method name, parameter and return type are same but one
method is in the parent class and another one in the child class.

4.which package is imported by defult and why?

Three packages are imported by default for each source file. First, the package with
no name. Second, the java.lang package. And third, the current package (the
package in which the current file is defined).

In every Java program there can be one unnamed package, which is simply a
package with no name. If you omit the package statement while writing the class
definition, the class name is placed into the default package, which has no name.
Java compiler automatically imports this package.

Second, the java.lang package is imported implicitly. Java is a pure object oriented
programming language where code is written in form of classes. These class
components are called types. Types in Java come in two flavours: built-in or
primitive types and types from components. Primitive types can be used directly
while component types must usually be ordered from a library by importing them
from the appropriate package. The technical term for ordering a component from a
library is bringing a component into scope. The Java standard libraries
includejava.lang package by default, which contains a number of components that
are used very commonly in Java programs. Java is useless without much of the
functionality in java.lang, that's why java.lang is implicitly imported by the compiler
for all programs.

However, one can import the same package or same class multiple times. If you
explicitly import java.lang in your program then neither compiler nor JVM complains
anything about it, but JVM internally loads the required class only once, no matter
how many times you import the same class.

Third, the current package is the one in which the class in execution is defined.
Current Java package is automatically imported by Java compiler.

5.what is difference between interface and abstract class?


Abstract class:
1) Abstract class can have abstract and non-abstractmethods.
2) Abstract class doesn't support multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.
4) Abstract class can have static methods, main method and constructor.
5) Abstract class can provide the implementation of interface.
6) The abstract keyword is used to declare abstract class.
7) Example:
public abstract class Shape{
public abstract void draw();
}

Interface class:

1) Interface can have only abstract methods.


2) Interface supports multiple inheritance.
3) Interface has only static and final variables
4) Interface can't have static methods, main method or constructor.
5) Interface can't provide the implementation of abstract class.
6) The interface keyword is used to declare interface.
7) Example:
public interface Drawable{
void draw();
}

6.can an abstract class be declared final?


No, Abstract method cannot be declared as final. This is because, abstract method has to be
overriden to provide implementation. If it is declared as final, it cannot be overridden

7.what do you understand by private,protected and public?

Public variables, are variables that are visible to all classes.

Private variables, are variables that are visible only to the class to which
they belong.

Protected variables, are variables that are visible only to the class to which
they belong, and any subclasses.

8.How are this() and super() used with constructors?


this() constructor is invoked within a method of a class, if the execution of the
constructor is to be done before the functionality of that method.

Example
void getValue()
{
this();

}

super() constructor is used within the constructor of the sub class, as the very first
statement. This process is used when the super class constructor is to be invoked
first, when the sub class object is instantiated everytime.

Example
class SubClass
{
SubClass()
{
super();
..
}
}

9.what is encapsulation in object oriented programming?

Encapsulation is an Object Oriented Programming concept that binds together the data and
functions that manipulate the data, and that keeps both safe from outside interference and
misuse. Data encapsulation led to the important OOP concept of data hiding.

10-what is differene b/w Array and Arraylist?


1) First and Major difference between Array and ArrayList in Java is that Array is
a fixed length data structure while ArrayList is a variable length Collection class.
You can not change length of Array once created in Java but ArrayList re-size itself
when gets full depending upon capacity and load factor. Since ArrayList is internally
backed by Array in Java, any resize operation in ArrayList will slow down
performance as it involves creating new Array and copying content from old array to
new array.

2) Another difference between Array and ArrayList in Java is that you can not
use Generics along with Array, as Array instance knows about what kind of type it
can hold and throws ArrayStoreException, if you try to store type which is not
convertible into type of Array. ArrayList allows you to use Generics to ensure type-
safety.

3) You can also compare Array vs ArrayList on How to calculate length of Array or
size of ArrayList. All kinds of Array provides length variable which denotes length of
Array while ArrayList provides size() method to calculate size of ArrayList in Java.

4) One more major difference between ArrayList and Array is that, you can not
store primitives in ArrayList, it can only contain Objects. While Array can contain
both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an
impression of storing primitives in ArrayList, it actually automatically converts
primitives to Object. e.g.

ArrayList<Integer> integerList = new ArrayList<Integer>();


integerList.add(1); //here we are not storing primitive in ArrayList, instead
autoboxing will convert int primitive to Integer object

5) Java provides add() method to insert element into ArrayList and you can simply
use assignment operator to store element into Array e.g. In order to store Object to
specified position use

Object[] objArray = new Object[10];


objArray[1] = new Object();
6) One more difference on Array vs ArrayList is that you can create instance
of ArrayList without specifying size, Java will create Array List with default size but
its mandatory to provide size of Array while creating either directly or indirectly by
initializing Array while creating it. By the way you can also initialize ArrayList while
creating it.

11.what is a jave class?Explain with simple example?


A class is nothing but a blueprint or a template for creating different objects which defines its
properties and behaviors. Java class objects exhibit the properties and behaviors defined by
its class. A class can contain fields and methods to describe the behavior of an object.
Example:
public class Cube {

int length;
int breadth;
int height;
public int getVolume() {
return (length * breadth * height);
}
}

12-if a method is declared as protected,where may the


method be accessed?

A protected method may only be accessed by classes or interfaces of the same package orby
subclasses of the class in which it is declared.

Long Question
Define a class weight to represent a weight in
tons,kilograms,and grams, and include a range of method
and constructors.Demonstrate this class by creating and
combining some class objects?

1. public class tkgWeight {

2. public static final int KG_PER_TON = 1000;

3. public static final int GRAMS_PER_KG = 1000;


4. public static final int GRAMS_PER_TON = GRAMS_PER_KG*KG_PER_TON;

5.

6. // private member variables

7. private int tons = 0;

8. private int kilograms = 0;

9. private int grams = 0;

10.

11. // Constructors:

12. public tkgWeight(double kg) {

13. this((int)Math.round(kg*GRAMS_PER_KG));

14. }

15.

16. public tkgWeight(int g) {

17. tons = g/GRAMS_PER_TON;

18. kilograms = (g - tons*GRAMS_PER_TON)/GRAMS_PER_KG;

19. grams = g - tons*GRAMS_PER_TON - kilograms*GRAMS_PER_KG;

20. }

21.

22. public tkgWeight(int t, int kg, int g) {

23. this(t*GRAMS_PER_TON + kg*GRAMS_PER_KG + g);

24. }

25.

26. public tkgWeight(){}

27.

28. // Methods

29. public String toString() {

30. return Integer.toString(tons) + "t " + kilograms + "kg " + grams + "g";
31. }

32.

33. public int toGrams() {

34. return tons*GRAMS_PER_TON + kilograms*GRAMS_PER_KG + grams;

35. }

36.

37. public double toKilograms() {

38. return ((double)toGrams())/GRAMS_PER_KG;

39. }

40.

41. public tkgWeight add(tkgWeight weight) {

42. return new tkgWeight(toGrams() + weight.toGrams());

43. }

44.

45. public tkgWeight subtract(tkgWeight weight) {

46. return new tkgWeight(toGrams() - weight.toGrams());

47. }

48.

49. public tkgWeight multiply(int n) {

50. return new tkgWeight(n*toGrams());

51. }

52.

53. public tkgWeight divide(int n) {

54. return new tkgWeight(toGrams()/n);

55. }

56.

57. // Compare two weights


58. // Return value is 1 if current greater than arg

59. // 0 if current equal to arg

60. // -1 if current less than arg

61. public int compare(tkgWeight weight) {

62. return greaterThan(weight) ? 1 : (equals(weight) ? 0 : -1);

63. }

64.

65. public boolean equals(tkgWeight weight) {

66. return toGrams() == weight.toGrams();

67. }

68.

69. public boolean lessThan(tkgWeight weight) {

70. return toGrams() < weight.toGrams();

71. }

72.

73. public boolean greaterThan(tkgWeight weight) {

74. return toGrams() > weight.toGrams();

75. }

76. }

This is Main Class to Test above tkgWeight Class.

view plainprint?

1. public class TestWeights {

2.

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

4. // Test the constructors

5. tkgWeight[] weights = {
6. new tkgWeight(274.65) , new tkgWeight(274),

7. new tkgWeight(274,2,3), new tkgWeight()

8. };

9.

10. //Display the weights

11. for(int i = 0 ; i < weights.length ; ++i)

12. System.out.println("Weight " + i + " is " + weights[i]);

13.

14. // Test the operations

15. System.out.println("Addition: " + weights[0] + " plus " + weights[1] + " is " + weights[0].add(weights[1]));

16. System.out.println("Subtraction: " + weights[0] + " minus " + weights[1] + " is " + weights[0].subtract(weight
s[1]));

17. System.out.println("Multiplication: "+ weights[0] + " times 10 is " + weights[0].multiply(10));

18. System.out.println("Division: " + weights[0] + " divided by 10 is " + weights[0].divide(10));

19.

20. // Test comparison methods

21. if(weights[0].greaterThan(weights[1])) {

22. System.out.println("Weight "+ weights[0] + " is greater than length " + weights[1]);

23. } else if(weights[0].lessThan(weights[1])) {

24. System.out.println("Weight "+ weights[0] + " is less than length " + weights[1]);

25. } else {

26. System.out.println("Weight "+ weights[0] + " is the same length as length " + weights[1]);

27. }

28.

29. // Compare successive weights using compare() method

30. String compareStr = null;

31. for(int i = 0 ; i < weights.length - 1 ; ++i) {


32. switch(weights[i].compare(weights[i+1])) {

33. case -1:

34. compareStr = " is less than length ";

35. break;

36. case 0:

37. compareStr = " is equal to length ";

38. break;

39. case 1:

40. compareStr = " is greater than length ";

41. break;

42. }

43. System.out.println("Weight " + weights[i] + compareStr + weights[i+1]);

44. }

45. }

46. }

47.

48. Output of Above Java Program

49. Weight 0 is 0t 274kg 650g


50. Weight 1 is 0t 0kg 274g
51. Weight 2 is 274t 2kg 3g
52. Weight 3 is 0t 0kg 0g
53. Addition: 0t 274kg 650g plus 0t 0kg 274g is 0t 274kg 924g
54. Subtraction: 0t 274kg 650g minus 0t 0kg 274g is 0t 274kg 376g
55. Multiplication: 0t 274kg 650g times 10 is 2t 746kg 500g
56. Division: 0t 274kg 650g divided by 10 is 0t 27kg 465g
57. Weight 0t 274kg 650g is greater than length 0t 0kg 274g
58. Weight 0t 274kg 650g is greater than length 0t 0kg 274g
59. Weight 0t 0kg 274g is less than length 274t 2kg 3g
60. Weight 274t 2kg 3g is greater than length 0t 0kg 0g
Write a java program thath has two classes point and
circle.circle class is the subclass of the point
class.Demonstrateuprcasting and downcasting by using
circle class?

You might also like