Inheritance in Java
Inheritance in Java
Guys,
Welcome you all to this page. Inheritance is one of the important concepts in java what all software professionals and all computer science students should be strong enough to code in. Inheritance in Java is also an important question asked in majority of the interviews and in exams as well. Since guys find hard and quiet confusing in this concept of object oriented programming studies, I described the easiest way on the exact concept with examples in this page. Let me share my knowledge on this with you.
Inheritance means one class acquiring the properties from another class. A class which is derived from other class is a subclass or derived class or child class and the class from which the child class is derived is called as a super class or the base class or the parent class. Inheritance is an important concept in object oriented programming language. There are many types of inheritances in object oriented design. They are single inheritance, multiple inheritance, multilevel inheritance and hierarchical inheritance. But here we are going to discuss about two types namely the single inheritance and the multilevel inheritance. Also an important concept in java what you should know is java does not support multiple inheritance. Because, when a class inherits from more than one class, it will lead to an inheritance path ambiguity problem which is commonly called as a diamond problem. But the concept of multiple inheritances can be achieved in java by using the concept of interfaces. Interfaces are friendly and are advanced which replaces the concept of multiple inheritances in java. Two important concepts of inheritances namely single inheritance and multilevel inheritance are explained in detail below. Please have a look.
Single Inheritance
The concept of single inheritance is illustrated below. I.e. one class is derived from another class. I.e. one class acquiring the properties from another class. Here insurance is the parent class and trading is the child class. Child class extends the parent class I.e. inherits with the parent class using the keyword extends as shown in the below example.
Class Class in Java Examples or Inheritance Examples
class insurance
{
int cash;
int deposit_id;
int substitute (int a, int b)
{
cash=a;
deposit_id=b;
return (0);
}
void display ()
{
System.out.println ("Please claim the insurance");
}
}
class trading extends insurance
{
public static void main (String args [])
{
insurance ins = new insurance ();
ins.substitute (5000,123);
ins.display ();
}
void screen ()
{
System.out.println ("The insurance amount deposited is valid");
}
}
Multilevel Inheritance
When a child class is derived from a derived class, then it is known as multilevel inheritance.
class insurance
{
//implementations
}
class trading extends insurance
{
//implementations
}
class bank extends trading
{
//implementations
}
Wow. That is so elegant and logical and clearly explained. Keep it up! I follow up your blog for future post.
ReplyDeleteRegards,
Java Online Training | Java Online Training in India | Java Online Training India | Java Online Training in Hyderabad | Java Online Training Hyderabad | Java Training in Hyderabad | Java Training in India | Java Training Institutes in India | Java Training Institutes in Hyderabad | Java Course in Hyderabad | Java Training | Learn Java Online | Online Java Training | Best Java online Training Institutes in Hyderabad | Best Java Training Institutes in Hyderabad | Best Institutes for Java | Java Institutes in Hyderabad | Best Institutes for Java in Hyderabad | Learn Java | Java Training Institutes in Hyderabad | Java Certification | Java Certification Training | Java Certification Training in Hyderabad | Java Certification Training in India