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

Facts about inheritance in Java.

Typology: Study notes

2020/2021

Uploaded on 03/13/2021

Iniobong
Iniobong 🇳🇬

1 document

1 / 68

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT - 3
Inheritance
Method Overriding
Constructor Calling
Runtime Polymorphism
Usage of Final
Package
Interfaces
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

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();