Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Java OOPS Concepts Summary

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java OOPS Concepts Summary

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java OOPS Concepts Summary

OOPS with JAVA


BCS306A
Question Bank
1. Explain different type of inheritance in JAVA along with sample programs.
2. What is inheritance? What is the advantage of inheritance? Develop a JAVA
program to create a class named shape. Create three sub classes namely:
circle, triangle and square, each class has two member functions named
draw () and erase (). Demonstrate polymorphism concepts by developing
suitable methods, defining member data and main program.
3. With suitable code segments illustrate the various uses of ‘final’ keyword?
4. What is abstract class? Explain with example
5. What is method overriding? Explain how method overriding supports
polymorphism with example.
6. Demonstrate dynamic dispatch using abstract class in JAVA.
7. What is an interface? Illustrate with an example how multiple Inheritances is
implemented in Java.
8. Define package. Write a Java program to create and import the package to
make use of class declared in package
9. Write a JAVA program to demonstrate the producer consumer problem using
synchronized threads.
10. Define a thread. Explain the various ways the threads can be created in Java.
11. With a sample program to describe the exception handling mechanism in
java?
12. What is enumeration? Explain values() of valueOf() methods
13. What is autoboxing and autounboxing in arithmetic expression? Explain
with example

Here's a breakdown of your OOPS with JAVA (BCS306A) Question Bank:

1. Explain different types of inheritance in JAVA along with sample programs.

Types of Inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance (not directly supported in Java)
Multiple Inheritance (achieved using interfaces in Java)

Example:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/6
java

// Single Inheritance
class Parent {
void display() {
System.out.println("This is the Parent class");
}
}
class Child extends Parent {
void show() {
System.out.println("This is the Child class");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Child c = new Child();
c.display();
c.show();
}
}

2. What is inheritance? What is the advantage of inheritance?

Definition: Inheritance allows one class (child/subclass) to acquire the properties and methods of
another class (parent/superclass).
Advantages:
Reusability
Extensibility
Code maintainability
Method overriding and polymorphism

Polymorphism Example:

java

class Shape {
void draw() {
System.out.println("Drawing Shape");
}
void erase() {
System.out.println("Erasing Shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
void erase() {
System.out.println("Erasing Circle");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Drawing Triangle");
}
void erase() {
System.out.println("Erasing Triangle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing Square");
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/6
void erase() {
System.out.println("Erasing Square");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape s;
s = new Circle();
s.draw();
s.erase();

s = new Triangle();
s.draw();
s.erase();

s = new Square();
s.draw();
s.erase();
}
}

3. With suitable code segments illustrate the various uses of the `final` keyword.

Final class: Prevents inheritance.


Final method: Prevents overriding.
Final variable: Makes the variable constant.

Example:

java

final class FinalClass {


void display() {
System.out.println("Final Class cannot be extended");
}
}

class FinalMethodExample {
final void show() {
System.out.println("Final Method cannot be overridden");
}
}

public class FinalDemo {


public static void main(String[] args) {
final int value = 10;
System.out.println("Final variable: " + value);
// value = 20; // Error: Cannot modify final variable
}
}

4. What is abstract class? Explain with example.

Abstract Class: A class that cannot be instantiated and may contain abstract methods (methods
without a body).

Example:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/6
java

abstract class Animal {


abstract void sound(); // Abstract method
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class AbstractExample {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}

5. What is method overriding? How does it support polymorphism?

Definition: When a subclass provides a specific implementation for a method already defined in its
superclass.
Polymorphism: At runtime, the appropriate method is invoked depending on the object.

Example:

java

class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class MethodOverriding {
public static void main(String[] args) {
Animal a = new Dog(); // Runtime Polymorphism
a.sound();
}
}

6. Demonstrate dynamic dispatch using abstract class in JAVA.

Dynamic Dispatch: Method call resolved at runtime.

Example:

java

abstract class Shape {


abstract void draw();
}
class Circle extends Shape {
void draw() {

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/6
System.out.println("Drawing Circle");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Drawing Triangle");
}
}
public class DynamicDispatch {
public static void main(String[] args) {
Shape s;
s = new Circle();
s.draw();
s = new Triangle();
s.draw();
}
}

7. What is an interface? Illustrate multiple inheritance using interfaces.

Definition: An interface defines a contract with abstract methods.

Example:

java

interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
public class InterfaceExample {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

8. Define package. Write a Java program to create and import a package.

Package: A namespace to organize classes.

Example:

java

// File: mypack/MyClass.java
package mypack;
public class MyClass {
public void display() {
System.out.println("This is MyClass in package");
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/6
}
}

// File: Main.java
import mypack.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}

9. Producer-Consumer Problem using synchronized threads.

Example:

java

class Q {
int n;
boolean valueSet = false;
synchronized void put(int n) {
while (valueSet) try { wait(); } catch (Exception e) {}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
synchronized int get() {
while (!valueSet) try { wait(); } catch (Exception e) {}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) { this.q = q; new Thread(this).start(); }
public void run() {
int i = 0;
while (true) q.put(i++);
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) { this.q = q; new Thread(this).start(); }
public void run() {
while (true) q.get();
}
}
public class PCExample {
public static void main(String[] args) {
Q q = new Q();
new Producer(q);
new Consumer(q);
}
}

Let me know if you need explanations or solutions for other questions! 😊

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/6

You might also like