0% found this document useful (0 votes)
11 views22 pages

Java OOP Concepts and Important Questions

The document outlines important questions and programming concepts related to Object-Oriented Programming with Java, including garbage collection, static variables, method overloading, abstract classes, inheritance, and interfaces. It provides definitions, examples, and code snippets to illustrate these concepts. Additionally, it discusses the use of the super keyword, method overriding, and the implementation of multiple inheritance through interfaces.

Uploaded by

pp0077586
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Java OOP Concepts and Important Questions

The document outlines important questions and programming concepts related to Object-Oriented Programming with Java, including garbage collection, static variables, method overloading, abstract classes, inheritance, and interfaces. It provides definitions, examples, and code snippets to illustrate these concepts. Additionally, it discusses the use of the super keyword, method overriding, and the implementation of multiple inheritance through interfaces.

Uploaded by

pp0077586
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BCS306A & Object Oriented Programming with JAVA

Important Questions – IA2

1. Define Garbage collection(2)

Java takes an approach for deallocation of memory automatically. The


technique that accomplishes this is called garbage collection. It works like
this: when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.

2. Write separate programs that shows the implementation of


(i) static variable and static member Function static block
concept(8)

Normally, a class member must be accessed only in conjunction with an


object of

its class. However, it is possible to create a member that can be used by


itself,

without reference to a specific instance.

To create such a member, precede its declaration with the keyword static.

 When a member is declared static, it can be accessed before any objects of


its

class are created, and without reference to any object.

 You can declare both methods and variables to be static. The most
common

example of a static member is main( ). main( ) is declared as static because


it

must be called before any objects exist.

Instance variables declared as static are, essentially, global variables. When

objects of its class are declared, no copy of a static variable is made. Instead,
all

instances of the class share the same static variable.


3. Illustrate ‘static’ keyword and its use.(2)

Methods declared as static have several restrictions:

• They can only directly call other static methods of their class.

• They can only directly access static variables of their class.

• They cannot refer to this or super in any way. (The keyword super relates
to

inheritance and is described in the next chapter.)

4. Write a program to display the factorial of a number.(10)

// A simple example of recursion.

class Factorial {

// this is a recursive method

int fact(int n) {

int result;

if(n==1) return 1;

result = fact(n-1) * n;

return result;

class Recursion {

public static void main(String[] args) {

Factorial f = new Factorial();

[Link]("Factorial of 3 is " + [Link](3));

[Link]("Factorial of 4 is " + [Link](4));

[Link]("Factorial of 5 is " + [Link](5));


}

5. Define Finalize method. What is the use of finalize method?(2)

A field can be declared as final. Doing so prevents its contents from being
modified, making

it, essentially, a constant. This means that you must initialize a final field
when it is declared.

You can do this in one of two ways: First, you can give it a value when it is
declared. Second,you can assign it a value within a constructor

[Link] to create an inner class?(2)

It is possible to define a class within another class; such classes are known
as nested classes

There are two types of nested classes: static and non-static. A static nested
class is one

that hasthe static modifier applied. Because it isstatic, it must accessthe


non-static members

of its enclosing classthrough an object

The second type of nested class is the inner class. An inner class is a non-
static nested

class

7 Write a program for method overloading mechanism.(8)

class OverloadDemo {

void test() {

[Link]("No parameters");

}
// Overload test for one integer parameter.

void test(int a) {

[Link]("a: " + a);

// Overload test for two integer parameters.

void test(int a, int b) {

[Link]("a and b: " + a + " " + b);

// Overload test for a double parameter

double test(double a) {

[Link]("double a: " + a);

return a*a;

class Overload {

public static void main(String[] args) {

OverloadDemo ob = new OverloadDemo();

double result;

// call all versions of test()

[Link]();

[Link](10);

[Link](10, 20);[Type text]

Page 20

result = [Link](123.25);

[Link]("Result of [Link](123.25): " + result);


}

This program generates the following output:

No parameters

a: 10

a and b: 10 20

double a: 123.25

Result of [Link](123.25): 15190.5625

 In Java, it is possible to define two or more methods within the same class
that share the

same name, as long as their parameter declarations are different. When this
is the case,

the methods are said to be overloaded, and the process is referred to as


method

overloading.

 Method overloading is one of the ways that Java supports polymorphism.

 When an overloaded method is invoked, Java uses the type and/or number
of arguments

as its guide to determine which version of the overloaded method to actually


call.

6. Write a program to display the student name, roll no and age of a


student using class and object concept.(10)
Lab program

7. Describe Abstract classes with an example program. Also


describe the properties of abstract classes.(10)

Java abstract class is a class that can not be initiated by itself, it needs to be
subclassed by
another class to use its properties.

 An abstract class is declared using the “abstract” keyword in its class


definition.

 The main purpose of an abstract class is to provide a common structure for


its subclasses,

ensuring that they implement certain essential methods.

 An abstract class can also have constructors, data members, and static
methods

 To declare an abstract method, use this general form:

abstract type name(parameter-list);

Using an abstract class, you can improve the Figure class shown earlier.
Since there is no meaningful concept of

area for an undefined two-dimensional figure, the following version of the


program declares area( ) as abstract

inside Figure. This, of course, means that all classes derived from Figure
must override area( ).

// Using abstract methods and classes.

abstract class Figure {

double dim1;

double dim2;

Figure(double a, double b) {

dim1 = a;

dim2 = b;

// area is now an abstract method

abstract double area();

}
class Rectangle extends Figure {

Rectangle(double a, double b) {

super(a, b);

Par I// override area for rectangle

double area() {

[Link]("Inside Area for Rectangle.");

return dim1 * dim2;

class Triangle extends Figure {

Triangle(double a, double b) {

super(a, b);

// override area for right triangle

double area() {

[Link]("Inside Area for Triangle.");

return dim1 * dim2 / 2;

class AbstractAreas {

public static void main(String[] args) {

// Figure f = new Figure(10, 10); // illegal now

Rectangle r = new Rectangle(9, 5);

Triangle t = new Triangle(10, 8);


Figure figref; // this is OK, no object is created

figref = r;

[Link]("Area is " + [Link]());

figref = t;

[Link]("Area is " + [Link]());

8. Summarize the concept of supper classes and sub classes.(10)

It involves the creation of a general class (superclass) defining common


traits for a

group of related items.

 Other, more specific classes (subclasses) can then inherit from this
superclass, adding

unique elements while retaining the inherited traits.

To inherit a class, you simply incorporate the definition of one class into
another by

using the extends keyword.

// A simple example of inheritance.

// Create a superclass.

class A {

int i, j;

void showij() {

[Link]("i and j: " + i + " " + j);

// Create a subclass by extending class A.


class B extends A {

int k;

void showk() { [Link]("k: " + k);

void sum() {

[Link]("i+j+k: " + (i+j+k));

class SimpleInheritance {

public static void main(String[] args) {

A superOb = new A();

B subOb = new B();

// The superclass may be used by itself.

superOb.i = 10;

superOb.j = 20;

[Link]("Contents of superOb: ");

[Link]();

[Link]();

/* The subclass has access to all public members of

its superclass. */

subOb.i = 7;

subOb.j = 8;

subOb.k = 9;

[Link]("Contents of subOb: ");

[Link]();
[Link]();

[Link]();

[Link]("Sum of i, j and k in subOb:");

[Link]();

The output from this program isshown here:

Contents of superOb:

i and j: 10 20

Contents of subOb:

i and j: 7 8

k: 9

Sum of i, j and k in subOb:

i+j+k: 24

9. How to use the super keyword to invoke a superclass's variable,


constructor and methods. Illustrate with example program.(10)

In Java, the super keyword is used to refer to the immediate parent class
object.

 super has two general forms.

The first calls the superclass’ constructor.

 The second is used to access a member of the superclass that has been
hidden by a member

of a subclass

// A complete implementation of BoxWeight.


class Box {

private double width;

private double height;

private double depth;

// construct clone of an object

Box(Box ob) { // pass object to constructor

width = [Link];

height = [Link];

depth = [Link];

// constructor used when all dimensions specified

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

// constructor used when no dimensions specified

Box() {

width = -1; // use -1 to indicate

height = -1; // an uninitialized

depth = -1; // box

// constructor used when cube is created

Box(double len) {

width = height = depth = len;


}

// compute and return volume

double volume() {

return width * height * depth;

}}

// BoxWeight now fully implements all constructors.

class BoxWeight extends Box {

double weight; // weight of box

// construct clone of an object

BoxWeight(BoxWeight ob) { // pass object to constructor

super(ob);

weight = [Link];

// constructor when all parameters are specified

BoxWeight(double w, double h, double d, double m) {

super(w, h, d); // call superclass constructor

weight = m;

// default constructor

BoxWeight() {

super();

weight = -1;

// constructor used when cube is created

BoxWeight(double len, double m) {


super(len);

weight = m;

class DemoSuper {

public static void main(String[] args) {

BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);

BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);

BoxWeight mybox3 = new BoxWeight(); // default

BoxWeight mycube = new BoxWeight(3, 2);

BoxWeight myclone = new BoxWeight(mybox1);

double vol;

vol = [Link]();

[Link]("Volume of mybox1 is " + vol);

[Link]("Weight of mybox1 is " + [Link]);

[Link]();

vol = [Link]();

[Link]("Volume of mybox2 is " + vol);

[Link]("Weight of mybox2 is " + [Link]);

[Link]();

vol = [Link]();

[Link]("Volume of mybox3 is " + vol);

[Link]("Weight of mybox3 is " + [Link]);

[Link]();

Part Ivol = [Link]();


[Link]("Volume of myclone is " + vol);

[Link]("Weight of myclone is " + [Link]);

[Link]();

vol = [Link]();

[Link]("Volume of mycube is " + vol);

[Link]("Weight of mycube is " + [Link]);

[Link]();

This program generates the following output:

Volume of mybox1 is 3000.0

Weight of mybox1 is 34.3

Volume of mybox2 is 24.0

Weight of mybox2 is 0.076

Volume of mybox3 is -1.0

Weight of mybox3 is -1.0

Volume of myclone is 3000.0

Weight of myclone is 34.3

Volume of mycube is 27.0

Weight of mycube is 2.0

10. Describe interfaces & how to implement it with a Java


Program.(10)

Using the keyword interface, you can fully abstract a class’ interface from its

implementation. That is, using interface, you can specify what a class must
do, but not

how it does it.


Once it is defined, any number of classes can implement an interface. Also,
one class

can implement any number of interfaces.

By

providing the interface keyword, Java allows you to fully utilize the “one
interface,

multiple methods” aspect of polymorphism

access interface name {

return-type method-name1(parameter-list);

return-type method-name2(parameter-list);

type final-varname1 = value;

type final-varname2 = value;

//...

return-type method-nameN(parameter-list);

type final-varnameN = value;

class Client implements Callback {

// Implement Callback's interface

public void callback(int p) {

[Link]("callback called with " + p);

class Client implements Callback {

// Implement Callback's interface

public void callback(int p) {

[Link]("callback called with " + p);


}

void nonIfaceMeth() {

[Link]("Classes that implement interfaces " +

"may also define other members, too.");

11. Define Inheritance? With diagrammatic illustration and java


programs illustrate the different types of inheritance.(10)

In object-oriented programming, inheritance is a fundamental concept that


enables the

creation of hierarchical classifications.

 It involves the creation of a general class (superclass) defining common


traits for a

group of related items.


 Other, more specific classes (subclasses) can then inherit from this
superclass, adding

unique elements while retaining the inherited traits.

 In Java terminology, the inherited class is the superclass, and the inheriting
class is

the subclass.

12. Explain method overriding with suitable example.(8)

Method overriding occurs when a subclass provides a specific


implementation for a method that is

already defined in its superclass.

Conditions for Method Overriding:

Same method name

Same method type signature (parameters and return type)

Result of Method Overriding:

The method in the subclass is said to override the method in the superclass.

When the overridden method is called through the subclass, it always refers
to the version defined in

the subclass.

Key Point:

The version of the method defined by the superclass is hidden when called
through the subclass.

Consider the following:

// Method overriding.

class A {

int i, j;

A(int a, int b) {

i = a;
j = b;

// display i and j

void show() {

[Link]("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() {

[Link]("k: " + k);

}class Override {

public static void main(String[] args) {

B subOb = new B(1, 2, 3);

[Link](); // this calls show() in B

The output produced by this program is shown here:

k: 3
13. Whether a subclass can access all members of super class?
How?(2)

To access parent class variables


14.

class Parent
{
int a=10;
}
class child extends Parent
{
int a=20;
void display()
{
[Link](super.a);
[Link](a);
}
} class Supervariable
{
public static void main(String args[])
{
child obj=new child();
[Link]();
}
}
Output
10
20
15. Does java support multiple inheritances?(2)

Interfaces are designed to support dynamic method resolution at run time


and multiple inheritance

16. Explain multiple inheritances and how to perform multiple


inheritances in Java.(8)

interface DemoInterface {
void abstractMethod(); // abstract method
default void defaultMethod() { // default method
[Link]("Default method in interface");
}
}

class DemoClass implements DemoInterface {

@Override
public void abstractMethod() {
[Link]("Abstract method implemented.");
}

void normalMethod() { // normal method


[Link]("Normal method in class.");
}
}

Example program:
interface Greeting {

void sayHello(); // abstract method

// default method
default void sayGoodbye() {
[Link]("Goodbye from the interface!");
}
}

// Class implements the interface


class Person implements Greeting {

@Override
public void sayHello() {
[Link]("Hello! I am a person.");
}
}

public class DefaultMethodSimpleDemo {


public static void main(String[] args) {

Person p = new Person();

[Link](); // calls the class implementation


[Link](); // inherited default method from interface
}
}
Output
Hello! I am a person.
Goodbye from the interface!
17. What's the difference between an interface and an abstract
class?(2)
An abstract class can have both abstract and concrete methods, instance
variables, and constructors, while an interface traditionally contains only
abstract methods (though modern versions allow default and static
methods) and constants
18. What is the difference between classes and interface?(2)

o Interfaces are syntactically similar to classes, but they lack instance


variables, and, as

a general rule, their methods are declared without any body.

o In practice, this means that you can define interfaces that don’t make
assumptions

about how they are implemented.

19. Explain extending interfaces with a Program & Variables in a


Program.(8)

Class Client implements Callback {

// Implement Callback's interface

public void callback(int p) {

[Link]("callback called with " + p);

class Client implements Callback {

// Implement Callback's interface

public void callback(int p) {

[Link]("callback called with " + p);

void nonIfaceMeth() {

[Link]("Classes that implement interfaces " +


"may also define other members, too.");

You might also like