0% found this document useful (0 votes)
8 views8 pages

Java Module 3

Uploaded by

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

Java Module 3

Uploaded by

hecapa4359
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Object-Oriented Programming (OOP) Overview

OOP is a programming paradigm based on the concept of objects that contain data (fields)
and methods (functions).
Main OOP principles: Encapsulation, Inheritance, Polymorphism, Abstraction.

Advantages:

 Code reusability
 Modularity
 Easy maintenance
 Real-world modeling

Classes & Objects

Class

 Blueprint or template for creating objects.


 Contains fields (variables) and methods.

class Student {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
Object

 An instance of a class.

public class Main {


public static void main(String[] args) {
Student s1 = new Student(); // Object creation
s1.name = "Rahul";
s1.age = 20;
s1.displayInfo();
}
}

Constructors

 Special methods used to initialize objects.


 No return type (not even void).
 Same name as the class.

Java/Sem-3/Module-3/BCA
Default Constructor
class Student {
Student() {
System.out.println("Default constructor called");
}
}
Parameterized Constructor
class Student {
String name;
int age;

Student(String n, int a) {
name = n;
age = a;
}
}

Encapsulation (Getters & Setters)

 Wrapping data (variables) and methods into a single unit.


 Achieved using private fields and public getters/setters.

class BankAccount {
private double balance;

public void setBalance(double b) {


if (b > 0) balance = b;
}

public double getBalance() {


return balance;
}
}

Inheritance

 Allows a class (child) to acquire properties/methods of another class (parent).


 Syntax: class Child extends Parent

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

class Dog extends Animal {


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

public class Main {

Java/Sem-3/Module-3/BCA
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}

Types of Inheritance in Java

Java supports five main types of inheritance, but not all are implemented directly due to
multiple inheritance limitations (to avoid ambiguity).

Single Inheritance

 One child class inherits from one parent class.


 Promotes code reuse.

Example:

class Parent {
void display() {
System.out.println("Parent class");
}
}

class Child extends Parent {


void show() {
System.out.println("Child class");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.display();
c.show();
}
}

Multilevel Inheritance

 A chain of inheritance.
 One class inherits from another, which in turn inherits from another.

Example:

class Grandparent {

Java/Sem-3/Module-3/BCA
void method1() {
System.out.println("Grandparent");
}
}

class Parent extends Grandparent {


void method2() {
System.out.println("Parent");
}
}

class Child extends Parent {


void method3() {
System.out.println("Child");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.method1();
c.method2();
c.method3();
}
}

Hierarchical Inheritance

 Multiple child classes inherit from a single parent class.

Example:

class Parent {
void commonMethod() {
System.out.println("Common to all children");
}
}

class Child1 extends Parent {


void child1Method() {
System.out.println("Child 1");
}
}

class Child2 extends Parent {


void child2Method() {
System.out.println("Child 2");
}

Java/Sem-3/Module-3/BCA
}

public class Main {


public static void main(String[] args) {
Child1 c1 = new Child1();
c1.commonMethod();
c1.child1Method();

Child2 c2 = new Child2();


c2.commonMethod();
c2.child2Method();
}
}

Multiple Inheritance (Through Interfaces)

 A class implements multiple interfaces.


 Java does not support multiple inheritance with classes (to avoid the Diamond
Problem) but allows it via interfaces.

Example:

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 Main {


public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

Hybrid Inheritance

 Combination of two or more types of inheritance.


 Achieved using interfaces in Java.

Java/Sem-3/Module-3/BCA
Example:

interface A {
void methodA();
}

class B {
void methodB() {
System.out.println("Class B");
}
}

class C extends B implements A {


public void methodA() {
System.out.println("Interface A");
}
}

public class Main {


public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

Super keyword

 Refers to parent class members.

class Animal {
Animal() { System.out.println("Animal constructor"); }
}

class Dog extends Animal {


Dog() {
super(); // Calls parent constructor
System.out.println("Dog constructor");
}
}

Method Overriding

 Child class redefines a parent class method.

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

Java/Sem-3/Module-3/BCA
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}

Polymorphism

 Compile-time (Method Overloading)


Same method name but different parameter list.

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

 Runtime (Dynamic Binding)


Method calls determined at runtime using method overriding.

Abstraction

 Hiding implementation details, showing only essential features.


 Achieved using:
o Abstract classes
o Interfaces

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() { System.out.println("Drawing Circle"); }
}
Interface
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() { System.out.println("Bark"); }
}

Advanced OOP Concepts

Java/Sem-3/Module-3/BCA
static Keyword

 Belongs to the class, not the object.

class Counter {
static int count = 0;
Counter() { count++; }
}
final Keyword

 final variable → constant


 final method → cannot be overridden
 final class → cannot be inherited

this Keyword

 Refers to the current object.

class Student {
String name;
Student(String name) {
this.name = name;
}
}

Packages & Access Modifiers

Packages

 Group related classes.


 package packname;
 import packname.ClassName;

Access Modifiers

Modifier Same Class Same Package Subclass Other Packages

private ✅ ❌ ❌ ❌

default ✅ ✅ ❌ ❌

protected ✅ ✅ ✅ ❌

public ✅ ✅ ✅ ✅

Java/Sem-3/Module-3/BCA

You might also like