Java OOPS Concepts Summary
Java OOPS Concepts Summary
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();
}
}
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.
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/6
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/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();
}
}
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/6
}
}
// 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/6