Short Question: 1.what Are Instance Variable?
Short Question: 1.what Are Instance Variable?
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.
class Taxes
{
int count;
/*...*/
}
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.
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.
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.
Interface class:
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.
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();
..
}
}
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.
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.
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
int length;
int breadth;
int height;
public int getVolume() {
return (length * breadth * height);
}
}
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?
5.
10.
11. // Constructors:
13. this((int)Math.round(kg*GRAMS_PER_KG));
14. }
15.
20. }
21.
24. }
25.
27.
28. // Methods
30. return Integer.toString(tons) + "t " + kilograms + "kg " + grams + "g";
31. }
32.
35. }
36.
39. }
40.
43. }
44.
47. }
48.
51. }
52.
55. }
56.
63. }
64.
67. }
68.
71. }
72.
75. }
76. }
view plainprint?
2.
5. tkgWeight[] weights = {
6. new tkgWeight(274.65) , new tkgWeight(274),
8. };
9.
13.
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]));
19.
21. if(weights[0].greaterThan(weights[1])) {
22. System.out.println("Weight "+ weights[0] + " is greater than length " + 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.
35. break;
36. case 0:
38. break;
39. case 1:
41. break;
42. }
44. }
45. }
46. }
47.