0% found this document useful (0 votes)
3 views

java_oop_concepts_final

The document provides examples of various Java programming concepts including classes, constructors, destructors, inheritance types, method overloading and overriding, abstract classes, interfaces, static keyword, encapsulation, polymorphism, file handling, and exception handling. Each concept is illustrated with code snippets and their corresponding outputs. These examples serve as a foundational understanding of object-oriented programming in Java.

Uploaded by

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

java_oop_concepts_final

The document provides examples of various Java programming concepts including classes, constructors, destructors, inheritance types, method overloading and overriding, abstract classes, interfaces, static keyword, encapsulation, polymorphism, file handling, and exception handling. Each concept is illustrated with code snippets and their corresponding outputs. These examples serve as a foundational understanding of object-oriented programming in Java.

Uploaded by

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

1.

Class and Object

class Student {
String name;
int age;

void display() {
System.out.println("Name: " + name + ", Age: " + age);
}

public static void main(String[] args) {


Student s1 = new Student();
s1.name = "John";
s1.age = 20;
s1.display();
}
}

Output:
Name: John, Age: 20
2. Constructor

class Car {
String model;

Car(String m) {
model = m;
}

void display() {
System.out.println("Car Model: " + model);
}

public static void main(String[] args) {


Car car1 = new Car("Toyota");
car1.display();
}
}

Output:
Car Model: Toyota
3. Destructor (Finalizer)

class FinalizerDemo {
protected void finalize() {
System.out.println("Object is garbage collected");
}

public static void main(String[] args) {


FinalizerDemo obj = new FinalizerDemo();
obj = null;
System.gc();
}
}

Output:
Object is garbage collected (May vary depending on GC execution)
4. Single Inheritance

class Animal {
void sound() {
System.out.println("Animals make sound");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog barks");
}

public static void main(String[] args) {


Dog d = new Dog();
d.sound();
d.bark();
}
}

Output:
Animals make sound
Dog barks
5. Multilevel Inheritance

class Animal {
void eat() {
System.out.println("Animals eat food");
}
}

class Mammal extends Animal {


void walk() {
System.out.println("Mammals walk");
}
}

class Human extends Mammal {


void speak() {
System.out.println("Humans speak");
}

public static void main(String[] args) {


Human h = new Human();
h.eat();
h.walk();
h.speak();
}
}

Output:
Animals eat food
Mammals walk
Humans speak
6. Hierarchical Inheritance

class Animal {
void eat() {
System.out.println("Animals eat food");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


void meow() {
System.out.println("Cat meows");
}

public static void main(String[] args) {


Dog d = new Dog();
d.eat();
d.bark();

Cat c = new Cat();


c.eat();
c.meow();
}
}

Output:
Animals eat food
Dog barks
Animals eat food
Cat meows
7. Method Overloading

class MathOperations {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

public static void main(String[] args) {


MathOperations obj = new MathOperations();
System.out.println("Sum (int): " + obj.add(5, 10));
System.out.println("Sum (double): " + obj.add(5.5, 10.5));
}
}

Output:
Sum (int): 15
Sum (double): 16.0
8. Method Overriding

class Parent {
void show() {
System.out.println("Parent's show method");
}
}

class Child extends Parent {


void show() {
System.out.println("Child's show method");
}

public static void main(String[] args) {


Parent p = new Child();
p.show();
}
}

Output:
Child's show method
9. Abstract Class

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing Circle");
}

public static void main(String[] args) {


Shape s = new Circle();
s.draw();
}
}

Output:
Drawing Circle
10. Interface

interface Animal {
void makeSound();
}

class Dog implements Animal {


public void makeSound() {
System.out.println("Dog barks");
}

public static void main(String[] args) {


Dog d = new Dog();
d.makeSound();
}
}

Output:
Dog barks
11. Static Keyword

class StaticExample {
static int count = 0;

StaticExample() {
count++;
System.out.println("Object count: " + count);
}

public static void main(String[] args) {


new StaticExample();
new StaticExample();
new StaticExample();
}
}

Output:
Object count: 1
Object count: 2
Object count: 3
12. Encapsulation

class Person {
private String name;

public void setName(String n) {


name = n;
}

public String getName() {


return name;
}

public static void main(String[] args) {


Person p = new Person();
p.setName("John");
System.out.println("Name: " + p.getName());
}
}

Output:
Name: John
13. Polymorphism (Runtime)

class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}

public static void main(String[] args) {


Animal a = new Dog();
a.makeSound();
}
}

Output:
Dog barks
14. File Handling

import java.io.FileWriter;
import java.io.IOException;

class FileDemo {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
writer.write("Hello, File Handling in Java!");
writer.close();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("An error occurred");
}
}
}

Output:
File written successfully (A file named output.txt will be created)
15. Exception Handling

class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}

Output:
Cannot divide by zero

You might also like