0% found this document useful (0 votes)
44 views6 pages

Detailed Unit II Inheritance Interface Packages

The document covers key concepts in object-oriented programming, including inheritance, interfaces, and packages in Java. It explains various types of inheritance, method overriding, the use of the final keyword, and the structure of interfaces. Additionally, it discusses packages, their types, and how to create and access them in Java.

Uploaded by

poojalandge1295
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)
44 views6 pages

Detailed Unit II Inheritance Interface Packages

The document covers key concepts in object-oriented programming, including inheritance, interfaces, and packages in Java. It explains various types of inheritance, method overriding, the use of the final keyword, and the structure of interfaces. Additionally, it discusses packages, their types, and how to create and access them in Java.

Uploaded by

poojalandge1295
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/ 6

Unit II: Inheritance, Interface, and Packages

2.1 Inheritance

Inheritance is a mechanism in object-oriented programming (OOP) that allows one class

(subclass/child class) to acquire

the properties and behaviors (fields and methods) of another class (superclass/parent class). This

promotes code reuse

and establishes a hierarchical relationship between classes.

Types of Inheritance:

1. Single Inheritance:

- A subclass inherits from one superclass.

Example:

class Animal {

void eat() { System.out.println("Eating..."); }

class Dog extends Animal {

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

Dog d = new Dog();

d.eat(); // Output: Eating...

d.bark(); // Output: Barking...

2. Multilevel Inheritance:

- A class inherits from a subclass, forming a multi-level hierarchy.

Example:
class Animal { void eat() { System.out.println("Eating..."); } }

class Dog extends Animal { void bark() { System.out.println("Barking..."); } }

class Puppy extends Dog { void weep() { System.out.println("Weeping..."); } }

Puppy p = new Puppy();

p.eat(); // Output: Eating...

p.bark(); // Output: Barking...

p.weep(); // Output: Weeping...

3. Hierarchical Inheritance:

- Multiple classes inherit from a single superclass.

Example:

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

class Dog extends Animal { void bark() { System.out.println("Barking..."); } }

class Cat extends Animal { void meow() { System.out.println("Meowing..."); } }

Dog d = new Dog();

d.eat(); d.bark(); // Outputs: Eating... Barking...

Cat c = new Cat();

c.eat(); c.meow(); // Outputs: Eating... Meowing...

Method Overriding:

- When a subclass provides a specific implementation of a method already defined in its superclass.

- Example:

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

class Dog extends Animal {

@Override

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

}
Animal a = new Dog();

a.sound(); // Output: Bark

Final Keyword:

- Final Variables: Constants that cannot be modified once initialized.

final int MAX = 100;

- Final Methods: Methods that cannot be overridden by subclasses.

final void show() { System.out.println("Cannot override this method"); }

- Final Classes: Classes that cannot be extended.

final class Animal {}

The 'super' Keyword:

- Used to access superclass methods and constructors.

- Example:

class Animal {

Animal() { System.out.println("Animal Constructor"); }

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

class Dog extends Animal {

Dog() {

super(); // Calls superclass constructor

System.out.println("Dog Constructor");

@Override

void sound() {
super.sound(); // Calls superclass method

System.out.println("Bark");

Abstract Methods and Classes:

- Abstract Class: Cannot be instantiated and may contain abstract methods.

- Abstract Method: Declared without implementation, must be implemented by subclasses.

- Example:

abstract class Animal {

abstract void sound();

class Dog extends Animal {

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

Animal a = new Dog();

a.sound(); // Output: Bark

2.2 Interfaces

An interface in Java is a reference type, similar to a class, that can contain only abstract methods,

static methods,

and final variables. Interfaces specify what a class must do but not how it does it.

Defining and Implementing Interfaces:

- Example:

interface Animal {

void sound();
}

class Dog implements Animal {

public void sound() {

System.out.println("Bark");

Animal a = new Dog();

a.sound(); // Output: Bark

Accessing Interface Variables and Methods:

- Interface variables are public, static, and final by default.

- Methods in an interface are abstract by default.

Extending Interfaces:

- An interface can extend another interface using the 'extends' keyword.

- Example:

interface Animal { void sound(); }

interface Pet extends Animal { void play(); }

class Dog implements Pet {

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

public void play() { System.out.println("Playing..."); }

2.3 Packages

A package in Java is a namespace that organizes classes and interfaces. It helps avoid name

conflicts and enhances modularity.


Types of Packages:

1. Built-in Packages: Provided by Java (e.g., java.util, java.io).

2. User-defined Packages: Created by developers.

Naming and Creating Packages:

- Declare a package using the 'package' keyword:

package com.example;

Accessing Packages:

- Classes and interfaces from packages can be accessed using the 'import' statement.

Import Statement:

- 'import package.classname;' or 'import package.*;' to import specific or all classes.

Static Import:

- Allows access to static members of a class directly without class reference.

import static java.lang.Math.*;

Adding Classes and Interfaces to a Package:

- Place source files in the package directory and compile them.

You might also like