OOP CONCEPTS:
[Link]:
>a class can be defined as "subclass" of another class("superclass").
•SUBCLASS:
-Inherits:
all attributes of superclass
all methods of superclass
all associations of superclass
-can Add new functionality.
-can Use inherited functionality.
-can Override inherited functionality.
----------------
>Declaration: (keyword "extends")
------------------------------------
public class Person
{
private String name;
private int age;
}
------------------------------------
public class Employee extends Person
{
private int employeeID;
private int salary;
}
------------------------------------
-in MAIN method: (SUBCLASS references)
Employee employee1 = new Employee("John", 35, 253, 1600)
----------------------------------------------------------------------------------
-->if inheritance is not defined, the class extends the class "Object".
----------------------------------------------------------------------------------
•Inheritance Hierarchy:
>java class can have only one SUPERCLASS.
>java class can have an unlimited number of SUBCLASSES.
>there is no limit to the depth of the class tree.
>higher classes are more general and abstract.
>lower classes are more specific and concrete.
----------------
•Constructors in Inheritance:
>SUPERCLASS constractors must be called in the 1st line of constructor code.
>the keyword "super" calls the SUPERCLASS constructors(like "this" keyword).
-------------------------------------------------------------
//constructor of subclass Employee:
public Employee(String name, int age, int employeeID, salary)
{
super(name, age);
[Link] = employeeID;
[Link] = salary;
}
--------------------------------------------------------------
----------------
•Method Overriding:
when the implimentation of the method in SUPERCLASS does not provide the
functionality required in the SUBCLASS.
>the method in SUBCLASS must have the same signature as in the SUPERCLASS.
-> same method name + same parameters : change the content.
----------------
•Object References:
inheritance defines "a kind of" relationship.
->SUPERCLASS references: refer to any SUBCLASS object derived from SUPERCLASS.
Person P1 = new Employee ("Amy", 28, 487, 2000);
| |
SUPERCLASS SUBCLASS