Object -Oriented programming(OOP) in Java part 3, Lecture notes of Computer science

Some important topics in OOP JAVA

Typology: Lecture notes

2024/2025

Available from 09/19/2025

pranav-kamble-2
pranav-kamble-2 🇮🇳

16 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object -Oriented programming(OOP) in Java part 3 and more Lecture notes Computer science in PDF only on Docsity!

Chapter 3: Object-Oriented Programming in Java 3.1 Introduction to OOP Java is a pure object-oriented language (except for primitive types). ¢ OOP helps organize code into reusable objects. ¢ Real-world modeling — Everything is an object with properties and behaviors. 3.2 Principles of OOP 1. Encapsulation — Binding data (variables) and methods into a single unit (class). o Achieved using classes and access modifiers (private, public). 2. Abstraction — Hiding implementation details, showing only essential features. ° Achieved using abstract classes and interfaces. 3. Inheritance — Reusing properties and behaviors of one class in another. ° Achieved using extends keyword. 4. Polymorphism —- One task performed in different ways. ° Compile-time polymorphism — Method overloading. ° Runtime polymorphism — Method overriding. 3.4 Constructors in Java ¢ Special methods used to initialize objects. e¢ Same name as class, no return type. Types of Constructors: 1. Default Constructor - No parameters. 2. Parameterized Constructor — Accepts arguments. 3. Copy Constructor (not built-in, but can be created manually). Example: Java class Car { String model; int year; // Constructor Car(String m, int y){ model = m; year = y; } void show(){ System.out.printin(year + " " + model); } } class Test { public static void main(String[] args) { Car cl = new Car("Tesla", 2025); c1.show(); 3.6 Method Overriding (Runtime Polymorphism) ¢ Subclass provides its own implementation of a method from parent class. Java class Animal { void sound(¢) { System.out.println¢("Animal makes sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } class Test { public static void main(String[] args) { Animal a = new Dog(); // Upcasting a.sound(); // Dog barks } 3.7 Inheritance in Java ¢ One class (child) can acquire properties of another class (parent). Types: 1. Single Inheritance 2. Multilevel Inheritance 3. Hierarchical Inheritance XX Java does not support multiple inheritance (solved using interfaces). 3.8 this and super Keywords ¢ this — Refers to current object. * super — Refers to parent class object. Java class Parent { String name = “Parent”; } class Child extends Parent { String name = "Child"; void display(){ System.out.printlin( this.name); // child System.out.println(super.name); // Parent } } 3.9 Access Modifiers Java provides four access levels: 1. public - Accessible everywhere. 2. protected - Accessible in same package + subclasses. 3. default (no modifier) - Accessible only in same package. 4. private - Accessible only within the class.