Partial preview of the text
Download Inheritance and packages in Java part 4 and more Lecture notes Computer science in PDF only on Docsity!
Chapter 4: Inheritance and Packages in Java 4.1 What is Inheritance? * Inheritance is the process by which one class (child/subclass) acquires properties and behaviors of another class (parent/ superclass). * It promotes code reusability and method overriding. Syntax: Java class Parent { // fields and methods } class Child extends Parent { // additional fields and methods } 4.2 Types of Inheritance in Java Java supports different forms of inheritance: 1. Single Inheritance © One subclass inherits from one superclass. Java class Animal { void eat() { System.out.printin("Eating"); } } class Dog extends Animal { void bark() { System.out.printlin("Barking"); } } 4. Hybrid Inheritance © Combination of multiple inheritance types. ° Not directly supported in Java (to avoid ambiguity). Achieved using interfaces. SX Multiple Inheritance (a class extending more than one class) is not allowed in Java (because of the diamond problem). Instead, interfaces are used. 4.3 The super Keyword in Inheritance e Used to access parent class members. 1. Calling parent class variable/method Java class Animal { String color = "white"; } class Dog extends Animal { String color = "black"; void showColor(){ System.out.println(super.color); // parent variable } } 4.4 Method Overriding in Inheritance e A subclass provides its own implementation of a method from the superclass. Java class Animal { void sound() { System.out.println¢("Animal makes sound"); } } class Dog extends Animal { @O0verride void sound() { System.out.println("Dog barks"); } } 4.5 Final Keyword in Inheritance 1. final variable — value cannot be changed. 2. final method — cannot be overridden. 3. final class — cannot be inherited. Java final class Animal { } // cannot be extended 4.7 Interfaces in Java ¢ An interface is like a blueprint of a class. ¢ Contains only abstract methods (before Java 8), plus default and static methods (after Java 8). ¢ Supports multiple inheritance. Java interface Animal { void sound(); } class Dog implements Animal { public void sound() { System.out.println("Dog barks"); } } 4.8 What is a Package in Java? * A package is a group of related classes and interfaces. ¢ Used for modularity, code reusability, and avoiding name conflicts. Types of Packages 1. Built-in Packages — Predefined by Java (e.g., java.util, java.io, java.sql). Example: Java import java.util.Scanner; class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print¢"Enter name: "); String name = sc.nextLine(); System.out.printlin¢"Hello " + name); } } 4.9 Advantages of Packages ¢ Code Reusability — Once written, can be reused anywhere. ¢ Encapsulation - Classes grouped logically. ¢ Namespace Management — Avoids name conflicts. ¢ Security - Access control with access modifiers. 4.10 Example Program (Inheritance + Packages) Java package animals; abstract class Animal { abstract void sound(); } class Dog extends Animal { void sound() { System.out.printin("Dog barks"); } } class Cat extends Animal { void sound() { System.out.println("Cat meows"); } } // Test file import animals.*; class Test { public static void main(String[] args) { Animal a1 Animal a2 al.sound(); a2.sound(); new Dog(); new Cat{);