itft-inheritance in java

14
Inheritence

Upload: atul-sehdev

Post on 19-May-2015

409 views

Category:

Education


1 download

DESCRIPTION

Inheritence, Terminology, Inheritance in java, The class called Object, Super keyword, Example, Method Overriding, Method Overriding example, Abstract Class, Abstract Class Number and the Java Wrapper Classes, Final Method and Classes, Multiple Inheritance

TRANSCRIPT

Page 1: itft-Inheritance in java

Inheritence

Page 2: itft-Inheritance in java

Inheritance is a fundamental Object Oriented

concept

A class can be defined as a "subclass" of

another class.The subclass inherits all data attributes of its superclass

The subclass inherits all methods of its superclass

The subclass inherits all associations of its superclass

The subclass can:Add new functionality

Use inherited functionality

Override inherited functionality

Person

- name: String

- dob: Date

Employee

- employeeID: int

- salary: int

- startDate: Date

superclass:

subclass:

Terminology

Page 3: itft-Inheritance in java

Inheritance is declared using the "extends" keywordIf inheritance is not defined, the class extends a class called Object

Person

- name: String

- dob: Date

Employee

- employeeID: int

- salary: int

- startDate: Date

public class Person

{

private String name;

private Date dob;

[...]

public class Employee extends Person

{

private int employeID;

private int salary;

private Date startDate;

[...]

Employee anEmployee = new Employee();

Inheritance in java

Page 4: itft-Inheritance in java

At the very top of the inheritance tree is a class

called Object

All Java classes inherit from Object.All objects have a common ancestor

This is different from C++

The Object class is defined in the java.lang

packageExamine it in the Java API Specification

The class called Object

Page 5: itft-Inheritance in java

Superclass constructors can be called using the

"super" keyword.

It must be the first line of code in the constructor

If a call to super is not made, the system will

automatically attempt to invoke the no-argument

constructor of the superclass.

Super keyword

Page 6: itft-Inheritance in java

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;}}

Example

Page 7: itft-Inheritance in java

Method Overriding

• If a derived or sub class has a method found within its base class or super class, that method will override the base class’s method

• The keyword super can be used to gain access to superclass methods overridden by the base class

• A subclass method must have the same return type as the corresponding superclass method

Page 8: itft-Inheritance in java

public class Abc

{

void display()

{

System.out.println(“Hello”);

}

}

public class Xyz extends Abc Output

{

void display() World

{

System.out.println(“World”);

}

public static void main(string str[])

{

Xyz obj = new Xyz();

obj.display();

} }

Example

Page 9: itft-Inheritance in java

public class Abc

{

void display()

{

System.out.println(“Hello”);

}

}

public class Xyz extends Abc Output

{

void display() Hello

{ World

super.display(); //this call Abc display()

System.out.println(“World”);

}

public static void main(string str[])

{

Xyz obj = new Xyz();

obj.display();

} }

Continue...

Page 10: itft-Inheritance in java

Abstract Class

• An abstract class can have abstract methods, data fields, and concrete methods

• Abstract class differs from a concrete class in that

• An abstract class cannot be instantiated

• An abstract class can declare abstract methods, which must be implemented in its subclasses

Page 11: itft-Inheritance in java

Abstract Class Number and the Java Wrapper Classes

Chapter 3: Inheritance and Class Hierarchies 11

Page 12: itft-Inheritance in java

Final Method and Classes

Methods can be qualified with the final modifier

Final methods cannot be overridden.

This can be useful for security purposes.

Classes can be qualified with the final modifierThe class cannot be extended.

Page 13: itft-Inheritance in java

Multiple Inheritance

• Multiple inheritance: the ability to extend more than one class

• Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity

• Therefore, Java does not allow a class to extend more than one class

Page 14: itft-Inheritance in java