Object Oriented Programming in JAVA (BCA30108) Module-3 Notes PDF
Document Details
Uploaded by Deleted User
Brainware University, Kolkata
2024
BCA30108
Shrestha Majumder
Tags
Summary
These notes cover Object Oriented Programming in Java, focusing on Module 3 topics such as inheritance, packages, and dynamic method dispatch. The document is part of a 3rd semester undergraduate course.
Full Transcript
BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) Study Material (Object Oriented Programming in Java, BCA30108) _______________________...
BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) Study Material (Object Oriented Programming in Java, BCA30108) ___________________________________________________________________________________________ Table of Contents Definition of inheritance Types of inheritance Super class and subclasses including multilevel hierarchy Use of super and final keywords Dynamic method dispatch Use of abstract classes and methods Interfaces. Creation of packages Importing packages Member access for packages 1 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) Definition of Inheritance: Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes (subclasses or child classes) that inherit properties and behaviors from existing classes (superclasses or parent classes). This promotes code reusability and helps in creating a hierarchical structure of classes. Types of Inheritance 1. Single Inheritance: A subclass inherits from only one superclass. Single Inheritance Block Diagram 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 obj = new Child(); obj.display(); // Calling parent class method 2 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) obj.show(); // Calling child class method } } Output: This is the parent class. This is the child class. 2. Multiple Inheritance: A subclass inherits from multiple superclasses. (Not directly supported in Java, but can be achieved using interfaces). 3. Multilevel Inheritance: A subclass inherits from a subclass, creating a chain of inheritance. Multilevel Inheritance Block Diagram class Animal { void eat() { System.out.println("This animal eats food."); } } class Mammal extends Animal { void walk() { System.out.println("This mammal walks."); } } class Dog extends Mammal { 3 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) void bark() { System.out.println("The dog barks."); } } public class MultilevelInheritance { public static void main(String[] args) { Dog d = new Dog(); d.eat(); d.walk(); d.bark(); } } Output: This animal eats food. This mammal walks. The dog barks. 4. Hierarchical Inheritance: Multiple subclasses inherit from a single superclass. Hierarchical Inheritance Block Diagram class Animal { void eat() { System.out.println("This animal eats food."); } } class Dog extends Animal { void bark() { 4 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) System.out.println("The dog barks."); } } class Cat extends Animal { void meow() { System.out.println("The cat meows."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); dog.eat(); dog.bark(); cat.eat(); cat.meow(); } } Output: This animal eats food. The dog barks. This animal eats food. The cat meows. 5. Hybrid Inheritance: A combination of multiple inheritance types. Hybrid Inheritance Block Diagram 5 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) interface A { void displayA(); } interface B { void displayB(); } class C { void displayC() { System.out.println("Class C"); } } class D extends C implements A, B { public void displayA() { System.out.println("Interface A"); } public void displayB() { System.out.println("Interface B"); } } public class HybridExample { public static void main(String[] args) { D obj = new D(); obj.displayA(); obj.displayB(); obj.displayC(); } } Output: Interface A Interface B Class C Super Class and Subclasses Superclass: The parent class from which properties and behaviors are inherited. Subclass: The child class that inherits from the superclass. 6 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) Multilevel Hierarchy class Grandparent { //... } class Parent extends Grandparent { //... } class Child extends Parent { //... } Use of super Keyword To call a superclass constructor: class Child extends Parent { Child() { super(); // Call the default constructor of Parent } } To access a superclass member variable: class Child extends Parent { void method() { int x = super.x; // Access the x variable of Parent } } To call a superclass method: class Child extends Parent { void method() { super.method(); // Call the method() of Parent } } **Use of `final` Keyword** * **To prevent a class from being inherited:** final class FinalClass { //... 7 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) } To prevent a method from being overridden: class Parent { final void method() { //... } } To prevent a variable from being reassigned: class MyClass { final int x = 10; // Constant variable; cannot be changed. } Dynamic Method Dispatch (Polymorphism) Dynamic method dispatch allows you to call methods on objects whose actual type is determined at runtime, not compile time. This is achieved through method overriding. class Parent { void display() { System.out.println("Parent class method"); } } class Child1 extends Parent { void display() { System.out.println("Child1 class method"); } } class Child2 extends Parent { void display() { System.out.println("Child2 class method"); } } public class DynamicDispatchExample { public static void main(String[] args) { Parent obj; // Reference of Parent class obj = new Child1(); // Points to Child1 obj.display(); // Calls Child1's display() 8 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) obj = new Child2(); // Points to Child2 obj.display(); // Calls Child2's display() } } Output: Child1 class method Child2 class method Abstract Classes and Methods An abstract class cannot be instantiated directly. It can have abstract methods, which are declared without a body and must be implemented by subclasses. abstract class Shape { abstract void draw(); } class Circle extends Shape { @Override void draw() { System.out.println("Drawing a circle"); } } Interfaces An interface is a completely abstract class that defines a set of methods that a class must implement. interface Drawable { void draw(); } class Circle implements Drawable { @Override public void draw() { System.out.println("Drawing a circle"); } } 9 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) Packages A package is a namespace that organizes a set of related classes and interfaces. Creating Packages // Create a package named mypackage package mypackage; public class MyClass { //... } Importing Packages import mypackage.MyClass; public class Main { public static void main(String[] args) { MyClass obj = new MyClass(); } } Member Access for Packages Public: Accessible from any class in any package. Protected: Accessible within the package and subclasses. Private: Accessible only within the class. Default: Accessible within the package. Additional Notes Inheritance can be used to create complex object hierarchies. Use inheritance judiciously to avoid tight coupling between classes. Consider using interfaces for loose coupling and polymorphism. Well-designed packages can improve code organization and maintainability. Real-world Example: Vehicle Hierarchy To illustrate inheritance concepts, let's consider a real-world example: a vehicle hierarchy. // Superclass: Vehicle abstract class Vehicle { protected String brand; protected String model; 10 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) public Vehicle(String brand, String model) { this.brand = brand; this.model = model; } public abstract void start(); public abstract void stop(); } // Subclass: Car class Car extends Vehicle { public Car(String brand, String model) { super(brand, model); } @Override public void start() { System.out.println("Starting the car..."); } @Override public void stop() { System.out.println("Stopping the car..."); } } // Subclass: Motorcycle class Motorcycle extends Vehicle { public Motorcycle(String brand, String model) { super(brand, model); } @Override public void start() { System.out.println("Starting the motorcycle..."); } @Override public void stop() { System.out.println("Stopping the motorcycle..."); } 11 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata BACHELOR OF COMPUTER APPLICATIONS (HONOURS with RESEARCH) - 2023 (3rd Semester) Object Oriented Programming in JAVA(BCA30108) BCA3B 2024 – 2025(ODD) } In this example, Vehicle is the superclass, and Car and Motorcycle are subclasses. The Vehicle class defines common properties like brand and model, as well as abstract methods start() and stop(). The subclasses Car and Motorcycle inherit these properties and implement the abstract methods to provide specific behavior for their respective types of vehicles. By using inheritance, we can create a flexible and reusable object hierarchy that can be easily extended to accommodate new types of vehicles. 12 Shrestha Majumder Assistant Professor, Computational Sciences Department Brainware University, Kolkata