Docsity

Java programming. Facts about inheritance in java., Study notes of Java Programming

Partial preview of the text

Download Java programming. Facts about inheritance in java. and more Study notes Java Programming in PDF only on Docsity!

UNIT - 3

Inheritance

Method Overriding

Constructor Calling

Runtime Polymorphism

Usage of Final

Package

Interfaces

Inheritance

  • (^) Allows the creation of hierarchical classification
  • (^) To inherit a class , we use the extends keyword
  • (^) Can specify only one superclass for any subclass that you create
  • (^) Does not support inheritance of multiple superclass
  • (^) A class member that has been declared as private will remain private to its class. It is not accessible by any code outside its class, including subclasses.

public class Employee {

String name;

int age;

double salary;

String language;

String databaseTool;

public class Employee { String name; int age; double salary; public void printData(){ System.out.println("name: " + name); System.out.println("age: " + age); System.out.println("salary: " + salary); } } public class Programmer extends Employee { String language; public void printData(){ super.printData(); System.out.println("language: " + language); } } public class DatabasePro extends Employee { String databaseTool; public void printData(){ super.printData(); System.out.println("Database Tool: " + databaseTool); } }

3

These are all Animals so will make Animal – Super Class Instance Variables - Picture , Food , Hunger, Location Methods – makenoise() , eat() , Sleep(), roam()

Method overriding - Definition

  • (^) Whenever same method name is existing in both base class and derived class with same types of parameters or same order of parameters is known as method Overriding
  • (^) Advantage:
    • (^) Method Overriding is used to provide specific implementation of a method that is already provided by its super class.
    • (^) Method Overriding is used for Runtime Polymorphism

Rules

• method must have same name as in the

parent class.

• method must have same parameter as in the

parent class.

• must be IS-A relationship (inheritance)

• Example : Overidedemo.java

Overloading Overriding (^1) Whenever same method or Constructor is existing multiple times within a class either with different number of parameter or with different type of parameter or with different order of parameter is known as Overloading. Whenever same method name is existing multiple time in both base and derived class with same number of parameter or same type of parameter or same order of parameters is known as Overriding. (^2) Arguments of method must be different at least arguments. Argument of method must be same including order. (^3) Method signature must be different. Method signature must be same. (^4) Private, static and final methods can be overloaded. Private, static and final methods can not be override. (^5) Also known as compile time polymorphism or static polymorphism or early binding. Also known as run time polymorphism or dynamic polymorphism or late binding. (^6) Overloading can be exhibited both are method and constructor level. Overriding can be exhibited only at method label. (^7) The scope of overloading is within the class. The scope of Overriding is base class and derived class. (^8) Overloading can be done at both static and non-static methods. Overriding can be done only at non-static method. (^9) For overloading methods return type may or may not be same. For overriding method return type should be same. Difference between Overloading and Overriding

Using super to call base class constructor

  • (^) class BoxWeight extends Box {
  • (^) double weight; // weight of box
  • (^) // initialize width, height, and depth using super()
  • (^) BoxWeight(double w, double h, double d, double m) {
  • (^) super(w, h, d); // call superclass constructor
  • (^) weight = m;
  • (^) }
  • (^) }

Constructors Call

  • (^) When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy executed?
  • (^) In a class hierarchy, constructors complete their execution in order of derivation, from superclass to subclass.
  • (^) Even if super() is not given it calls the parameterless constructor of parent class
  • (^) Example : classcallingone.java

Animal an; an = myDog; an.roam();