Thursday 27 April 2017

Inheritance in Java

Inheritance is a language property specific to the object-oriented paradigm. Inheritance is used for a unique form of code-sharing by allowing you to take the implementation of any given class and build a new class based on that implementation. Let us say class B, starts by inheriting all of the data and operations defined in the class A. This new subclass can extend the behaviour by adding additional data and new methods to operate on it. Basically during programming inheritance is used for extending the existing property of a class. In other words it can be said that inheritance is “from generalization- to-specialization”. In this by using general class, class with specific properties can be defined.

Now let us take the example of Employee class, which is declared as:

class BankAccount
{
data members
member functions
}

data members and member functions of BankAccount class are used to display characteristics of Withdraw, Deposit, getBalance of objects of a BankAccount class.

Now suppose you want to define a SavingAccount class. In the SavingAccount class definitely you will have basic characteristics of a Bankaccount class mentioned above. In your SavingAccount class you can use the properties of BankAccount class, without any modification in them. This use of properties of BankAccount class in Saving Account class is called inheriting property of BankAccount class into SavingAccount class.

To inherit a class into another class extends keyword is used. For example, SavingAccount class will inherit BankAccount class as given below.
class SavingAccount extends BankAccount
{
data members
ember functions
}
In this example, SavingAccount declares that it inherits or “extends” BankAccount.

Visit our site for java training in jaipur


0 comments:

Post a Comment