Java Reference
In-Depth Information
You can define classes and then you can define child classes based on the this parent class.
Advantage of doing so is that you can reuse code which you have written for the parent class
in your child class. The other advantage is that you can build hierarchy of classes which
provides a useful structure for your code.
Let us see how we can define a parent class (also known as super class) and a child class
which inherits properties and behavior of the parent class.
Public class parent {
public int age;
public String language;
age = 44;
language = “English”
Public class child extends parent {
age = 20;
language = “French”;
You can see we have defined a child class “child” which is a child class of class “parent”. If
you have defined class variables and methods in parent class then they are inherited in child
class. you can see that variable “age” and “language” are defined in parent class “parent”.
Since we have declared that class “child” is a child class of “parent” through the keyword
“extends”; class members of parent class become class members of child class. You do not
have to declare the variables “age” and “language” in the “child” class. you can directly as-
sign values to these members.
Inheritance is useful as you can create similar classes for similar objects.
Search WWH ::

Custom Search