AOOP - UNIT II (1)

21CSC206P - Advanced Object Oriented Programming

Unit II Overview

  • Class Fundamentals

    • Declaring Objects

    • Object Reference

    • Introduction to Methods

    • Constructors

    • this Keyword

    • Method Overloading

    • Using Objects as Parameters

    • Returning Objects

    • Recursion

    • Access Control

    • Static Members

    • Final Variables

    • Inner Classes

    • Command Line Arguments

    • Variable Length Arguments

Inheritance

  • Super Class

  • Sub Class

  • The Keyword super

  • Protected Members

  • Calling Order of Constructors

  • Method Overriding

  • The Object Class

  • Abstract Classes and Methods

  • Using final with Inheritance

Defining Classes in Java

  • Class Basics

    • A class is a template for an object.

    • Instance Variables: defined within a class, are specific to each object.

    • Members: methods and variables within a class.

Syntax for Declaring a Class

class classname {
  type instance-variable1;
  type instance-variableN;
  type methodname1(parameter-list) {
    // body of method
  }
}

Example Classes

Simple Class: Box

class Box {
  double width;
  double height;
  double depth;
}
  • Instantiation: Box mybox = new Box(); creates an instance of Box.

Using the Box Class

class BoxDemo {
  public static void main(String args[]) {
    Box mybox = new Box();
    mybox.width = 10;
    mybox.height = 20;
    mybox.depth = 15;
    double vol = mybox.width * mybox.height * mybox.depth;
    System.out.println("Volume is " + vol);
  }
}
  • Output: Volume is 3000.0

Constructors

  • Purpose: Initialize objects of a class.

  • Types:

    • Default Constructor: no parameters.

    • Parameterized Constructor: with parameters for initializing values.

Constructor Syntax

  • Default Constructor:

class Box {
  double width;
  double height;
  double depth;
  Box() {
    width = 10;
    height = 10;
    depth = 10;
  }
}

Example with Constructors

class BoxDemo {
  public static void main(String args[]) {
    Box mybox1 = new Box();
    Box mybox2 = new Box();
    double vol1 = mybox1.volume();
    double vol2 = mybox2.volume();
    System.out.println("Volume 1 is " + vol1);
    System.out.println("Volume 2 is " + vol2);
  }
}
  • Output: Volume is 1000.0 for both boxes.

Method Overloading and Overriding

  • Overloading: Same method name but different parameters.

  • Overriding: Redefining a method in a subclass.

Methods and Parameters

  • Method Declaration: type name(parameter-list) { // body of method }

    • void if no value is returned.

Access Control in Java

  • Used to restrict or allow access to classes, methods, and variables.

  • Modifiers: Private, Default, Protected, and Public.

Example of Access Control Usage

  • Private: Can be accessed within the same class only.

  • Protected: Accessible in subclasses.

  • Public: Accessible from anywhere.

Inheritance Overview

  • Types of Inheritance:

    • Single

    • Multiple

    • Multilevel

    • Hierarchical

    • Hybrid

Keywords in Inheritance

  • super: Refers to immediate parent class.

  • Can be used to reference parent class attributes and methods.

Method Overriding Example

  • Bank Example:

class Bank {
  int getRateOfInterest() { return 0; }
}
class SBI extends Bank {
  int getRateOfInterest() { return 8; }
}

Java Interfaces

  • An interface is a contract that defines methods. Classes implement interfaces.

  • Allows achieving abstraction and multiple inheritance.

Example of Interface Implementation
interface Bank {
  float rateOfInterest();
}
class SBI implements Bank {
  public float rateOfInterest() { return 9.15f; }
}

Conclusion

  • Understanding of Object-Oriented Programming principles in Java is crucial for building robust applications.