Solo disponible en BuenasTareas
  • Páginas : 9 (2153 palabras )
  • Descarga(s) : 7
  • Publicado : 28 de junio de 2010
Leer documento completo
Vista previa del texto


● ● ● ● ● ● ● ● ● ●

What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain “super” keyword Overriding methods Hiding methods Hiding fields Type casting Final class and final methods

What is Inheritance?

What is Inheritance?

Inheritance is the concept of a child class (sub class) automaticallyinheriting the variables and methods defined in its parent class (super class). A primary feature of object-oriented programming along with encapsulation and polymorphism


Why Inheritance? Reusability

Benefits of Inheritance in OOP : Reusability

Once a behavior (method) is defined in a super class, that behavior is automatically inherited by all subclasses

Thus, you write amethod only once and it can be used by all subclasses.

Once a set of properties (fields) are defined in a super class, the same set of properties are inherited by all subclasses

A class and its children share common set of properties

A subclass only needs to implement the differences between itself and the parent.

How to derive a sub-class?

extends keyword
● ●

Toderive a child class, we use the extends keyword. Suppose we have a parent class called Person.
public class Person { protected String name; protected String address; /** * Default constructor */ public Person(){ System.out.println(“Inside Person:Constructor”); name = ""; address = ""; } . . . . }

extends keyword
● ●

Now, we want to create another class named Student Since astudent is also a person, we decide to just extend the class Person, so that we can inherit all the properties and methods of the existing class Person. To do this, we write,
public class Student extends Person { public Student(){ System.out.println(“Inside Student:Constructor”); } . . . . }

What You Can Do in a Sub-class

A subclass inherits all of the “public” and “protected”members (fields or methods) of its parent, no matter what package the subclass is in If the subclass is in the same package as its parent, it also inherits the package-private members (fields or methods) of the parent


What You Can Do in a Sub-class Regarding Fields

The inherited fields can be used directly, just like any other fields. You can declare new fields in thesubclass that are not in the super class You can declare a field in the subclass with the same name as the one in the super class, thus hiding it (not recommended). A subclass does not inherit the private members of its parent class. However, if the super class has public or protected methods for accessing its private fields, these can also be used by the subclass.

What You Can Do in aSub-class Regarding Methods
● ●

The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the super class, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the super class, thus hiding it. You can declare new methods in the subclass that are not inthe super class.


Object Class

Object class is mother of all classes
– –

Object Class

In Java language, all classes are sub-classed (extended) from the Object super class Object class is the only class that does not have a parent class

Defines and implements behavior common to all classes including the ones that you write
– – – –

getClass()equals() toString() ...


Class Hierarchy

A sample class hierarchy


Super class & Sub class

Super class (Parent class)

Any class above a specific class in the class hierarchy. Any class below a specific class in the class hierarchy.

Sub class (Child class)


Constructor Calling Chain

How Constructor method of a Super class gets called
tracking img