Notes For Oops
Notes For Oops
Ans:
Method Overloading Method Overriding
1 Method overloading is used to increase the readability of the program.
2 Method overloading is performed within class.
3 In case of method overloading, parameter must be different.
4 Method overloading is the example of compile time polymorphism.
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
The output produced by this program is shown here:
k: 3
2. Explain the followings with example
i) this ii) super iii) final iv) finalize
ANS: this can be used inside any method to refer to the current
object.
That is, this is always a reference to the object on which the
method was invoked.
consider the following version of Box( ):
// A redundant use of this.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
Box(double width, double height, double depth)
{
this.width = width;
this.height = height;
this.depth = depth;
}
The this keyword can be used to refer current
class instance variable.
If there is ambiguity between the instance
variables and parameters, this keyword
resolves the problem of ambiguity.
3. Discuss public, private, protected and default access modifier with example.
• ANS: Protected: Can be accessed by all the class & subclass in the same package and all
the subclasses in other package.
• Public: can be accessed any where.
• Default: Can be accessed by all the class & subclass within the same package.
• Private : can be accessed only within the class & thus completely hidden from outside the
class.
class ABC{
protected double num = 100;
protected int square(int a){
return a*a;
}
}
public class example{
public static void main(String args[]){
ABC obj = new ABC();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
4. Explain the following terms with respect to exception handling. i) try ii) catch iii) throw
iv) finally
Exception Handling Fundamentals
➢ Java exception handling is managed via five keywords:
try, catch, throw, throws, and finally.
➢ Program statements that you want to monitor for exceptions are contained within a try block.
If an exception occurs within the try block, it is thrown.
➢ Your code can catch this exception (using catch) and handle it.
➢ System-generated exceptions are automatically thrown by the Java run-time system.
To manually throw an exception, use the keyword throw.
➢ Any exception that is thrown out of a method must be specified as such by a throws clause.
➢ Any code that absolutely must be executed after a try block completes is put in a finally block.
➢ If a class implements more than one interface, the interfaces are separated with a comma.
➢ The methods that implement an interface must be declared public.
➢ Also, the type signature of the implementing method must match exactly the type
signature specified in the in terface definition
Example:
class c1 implements Example
{
public void print()
{
System.out.println(“Hello World”);
}
}.
In the first step, unreferenced objects are identified and marked as ready for garbage
collection.
In the second step, marked objects are deleted.
Optionally, memory can be compacted after the garbage collector deletes objects, so
remaining objects are in a contiguous block at the start of the heap
9. Explain package in java. List out all packages with short description
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Defining a Package
➢ Include a package command as the first statement in a Java source file.
➢ Any classes declared within that file will belong to the specified package.
➢ The package statement defines a name space in which classes are stored.
➢ Syntax: package package_name;
package pkg1[.pkg2[.pkg3]];
➢ Packages in Java are used to prevent naming conflicts, to control access, visibility
control mechanism, etc.
➢ Package in Java is nothing but a folder in directory structure.
➢ Java Packages are stored in hierarchical manner
void display()
{
/* print maxSpeed of base class
(vehicle) */
System.out.println("Maximum Speed: " +
super.maxSpeed);
}
}
In the above example, both base class and subclass have a member maxSpeed. We could
access maxSpeed of base class in sublcass using super keyword.
} class Geeks {
} class MyClass {
public static void main(String[] args)
{
Object o = class.forName(args[0]).newInstance();
System.out.println("Class created for" +
o.getClass().getName());
}
}
13. Wh
Package in java can be categorized in two form, built-in package and user-
defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net,
io, util, sql etc.
16. How does the Java run-time system know where to look for packages that you create?
MULTITHREADING
17. What is multithreading? List advantages of multithreading?
Multithreading in java is a process of executing multiple threads
simultaneously.