Java OOPS Concepts Summary (1)
Java OOPS Concepts Summary (1)
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/10
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();
}
}
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/10
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.
Example:
java
class FinalMethodExample {
final void show() {
System.out.println("Final Method cannot be overridden");
}
}
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/10
java
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();
}
}
Example:
java
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/10
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();
}
}
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();
}
}
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/10
}
}
// File: Main.java
import mypack.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}
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);
}
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/10
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 are detailed explanations and sample programs for questions 10 to 13:
10. Define a thread. Explain the various ways threads can be created in Java.
Definition of Thread:
A thread is the smallest unit of a process that runs concurrently within a program. Java provides
multithreading support to improve performance and efficiency.
Example:
java
Example:
java
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/10
t.start();
}
}
Example:
java
11. Describe the exception handling mechanism in Java with a sample program.
Key Concepts:
Example:
java
Output:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/10
vbnet
Enumeration (enum):
Methods:
Example:
java
enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
}
public class EnumExample {
public static void main(String[] args) {
// Using values() method
for (Days day : Days.values()) {
System.out.println(day);
}
Output:
sql
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
Selected day: MONDAY
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/10
Definition:
Example:
java
Output:
yaml
Autoboxed value: 10
Autounboxed value: 20
Sum: 30
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/10