Amity School of Engineering: B. Tech Java Programming

Amity School of Engineering: B. Tech Java Programming

Amity School of Engineering
B.Tech., CSE(5th Sem.) & ECE(3rd Sem.)

Java Programming
Topic: Inheritance


– –

– –

– –

Figure: The Employee Class

– – –

– –

Figure: The Employee Class Diagram

EmployeeWithTerritory extends Employee class

– – –

– – –
Figure: Relationship between Employee & EmployeeWithTerritory Classes

• Base class: provides basis for inheritance
– Also called parent or superclass – Example: Employee

• Derived class: inherits data and methods from base class
– Also called child or subclass – Example: EmployeeWithTerritory

• extends keyword achieves inheritance in Java • extends used in first line of class declaration
public class EmployeeWithTerritory extends Employee

• Employee is superclass to EmployeeWithTerritory subclass
– Subclass gets public data and methods from Employee – Subclass may have additional data and methods

• Inheritance is one-way: child inherits from parent

Figure: The EmployeeWithTerritory Classes

Method Overriding

• Polymorphism: a feature of object-oriented design
– Permits many implementations under one name – Sometimes associated with operator overloading (but not in java) • Example: Plus (+) sign could mean addition or concatenation

• Method overriding is a type of polymorphism
– Applies to identical method headers (not overloading) in the multiple classes(under inheritance)

• Subtype polymorphism: child overrides parent

• As we know that Constructors have same name as class itself • Inheritance adds complexity to object construction • Superclass and subclass constructors called to create subclass • Superclass constructor executes before subclass constructor
public class ASuperClass { public ASuperClass() { System.out.println("In superclass constructor"); } } public class ASubClass extends ASuperClass { public ASubClass() { System.out.println("In subclass constructor"); } } public class DemoConstructors {...

Similar Essays