اتحاد طلبة هندسة الحاسوب والشبكات - المدونه الرسميه C.N.E : Object Oriented Programming Using Java-10

المشاركات الشائعة

Object Oriented Programming Using Java-10



Inheritance

It is the way where you can make a relationship between two classes, where the inheriting class is the child & the inherited class is the parent.

The child class uses the code of the parent class even without creating an object from the parent, while parent cannot use the code of child unless an object is created. This is similar to humanity, where the son inherits traits from his parents but not vice versa.

The inheritance makes use of the principle of re-usability, where a code is used many times without the need to re-write it again.
This happens because the child gets access to the methods of the parent, means you don't need to write a method existing in the parent into the child again to make it usable by the child.

In Java, a parent class is called "super class" while a child class is called "sub class".
A class can inherit only one class in Java.

The general syntax of inheritance is:

public sub_class extends super_class
{ sub-class code } 

where:

public: access modifier.
sub_class: the name of the sub class (obeys variable name rules).
extends: a reserved keyword used for inheritance.
super_class: the name of the super class (obeys variable name rules).

But limitations for access by sub class to the super class occurs sometimes.
This happens when some instant variables or methods declared as private in super class.
This case, the sub class cannot get a direct access to these methods and instant variables. 

How to overcome this problem & An example about inheritance are to be discussed next lesson ... 

سامر المناصرة




< >

ليست هناك تعليقات:

إرسال تعليق