Docsity

Java Inheritance and Polymorphism: Handout 5-1 and 5-2 for CS-506 Web Design & Development, Lecture notes of Web Design and Development

Toggle sidebar
bg1
Handout 5-1
Web Design & Development CS-506
- 51 -
Inheritance
In general, inheritance is used to implement a “is-a” relationship. Inheritance saves code
rewriting for a client thus promotes reusability.
In java parent or base class is referred as super class while child or derived class is
known as sub class.
Comparison with C++
Java only supports single inheritance. As a result a class can only inherit from one
class at one time.
Keyword extends is used instead of “:” for inheritance.
All functions are virtual by default
All java classes inherit from Object class (more on it later).
To explicitly call the super class constructor, use super keyword. It’s important to
remember that call to super class constructor must be first line.
Keyword super is also used to call overridden methods.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Inheritance and Polymorphism: Handout 5-1 and 5-2 for CS-506 Web Design & Development and more Lecture notes Web Design and Development in PDF only on Docsity!

Web Design & Development CS-

Inheritance

In general, inheritance is used to implement a “is-a” relationship. Inheritance saves code

rewriting for a client thus promotes reusability.

In java parent or base class is referred as super class while child or derived class is

known as sub class.

Comparison with C++

ƒ Java only supports single inheritance. As a result a class can only inherit from one

class at one time.

ƒ Keyword extends is used instead of “ : ” for inheritance.

ƒ All functions are virtual by default

ƒ All java classes inherit from Object class (more on it later).

ƒ To explicitly call the super class constructor, use super keyword. It’s important to

remember that call to super class constructor must be first line.

ƒ Keyword super is also used to call overridden methods.

Web Design & Development CS-

Example Code: using inheritance

We’ll use three classes to get familiar you with inheritance. First one is Employee class.

This will act as super class. Teacher class will inherit from Employee class and Test class

is driver class that contains main method. Let’s look at them one by one

class Employee {

protected int id; protected String name;

//parameterized constructor public Employee(int id, String name){ this.id = id; this.name = name; } //default constructor public Employee(){

// calling parameterized constructor of same (Employee) // class by using keyword this

this (10, “not set”); }

//setters public void setId (int id) { this.id = id; }

public void setName (String name) { this.name = name; }

//getters public int getId () { return id; }

public String getName () { return name; }

// displaying employee object on console public void display(){ System.out.println(“in employee display method”);

System.out.println("Employee id:" + id + " name:" + name); }

//overriding object’s class toString method public String toString() { System.out.println(“in employee toString method”); return "id:" + id + "name:" + name; }

}//end class

Web Design & Development CS-

Objects of Employee & Teacher class are created inside main method in Test class. Later

calls are made to display and toString method using these objects.

class Test {

public static void main (String args[]){

System.out.println("making object of employee"); Employee e = new Employee(89, "khurram ahmad");

System.out.println("making object of teacher"); Teacher t = new Teacher (91, "ali raza", "phd");

e.display(); //call to Employee class display method t.display(); //call to Teacher class display method

// calling employee class toString method explicitly System.out.println("Employee: " +e.toString());

// calling teacher class toString implicitly

System.out.println("Teacher: " + t);

} //end of main

}//end class

Output

Web Design & Development CS-

Object – The Root Class

The Object class in Java is a superclass for all other classes defined in Java's class

libraries, as well as for user-defined Java classes. For user defined classes, its not

necessary to mention the Object class as a super class, java does it automatically for you.

The class Hierarchy of Employee class is shown below. Object is the super class of

Employee class and Teacher is a subclass of Employee class. We can make another class

Manager that can also extends from Employee class.

Employe

Teacher Manager

Object

Web Design & Development CS-

Output

Web Design & Development CS-

Type Casting

In computer science, type conversion or typecasting refers to changing an entity of one

datatype into another. Type casting can be categorized into two types

1. Up-casting

ƒ Converting a smaller data type into bigger one

ƒ Implicit – we don’t have to do something special

ƒ No loss of information

ƒ Examples of

— Primitives

int a = 10;

double b = a;

— Classes

Employee e = new Teacher( );

2. Down-casting

ƒ Converting a bigger data type into smaller one

ƒ Explicit – need to mention

ƒ Possible loss of information

ƒ Examples of

— Primitives

double a = 7.65;

int b = (int) a;

— Classes

Employee e = new Teacher( ); // up-casting

Teacher t = (Teacher) e; // down-casting