0% found this document useful (0 votes)
3 views14 pages

Array Inheritance

The document provides an overview of arrays and inheritance in Java, explaining the types of arrays and the concept of inheritance as a mechanism for code reusability and method overriding. It details various types of inheritance, including single, multilevel, hierarchical, and hybrid inheritance, along with examples for each type. Additionally, it notes that Java does not support multiple inheritance through classes to avoid ambiguity, but it can be achieved using interfaces.

Uploaded by

sivaparu2304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Array Inheritance

The document provides an overview of arrays and inheritance in Java, explaining the types of arrays and the concept of inheritance as a mechanism for code reusability and method overriding. It details various types of inheritance, including single, multilevel, hierarchical, and hybrid inheritance, along with examples for each type. Additionally, it notes that Java does not support multiple inheritance through classes to avoid ambiguity, but it can be achieved using interfaces.

Uploaded by

sivaparu2304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ARRAY

2 marks
• An array is a collection of elements of same datatype. It has a fixed size
• Its elements are accessed using index values.
• contiguous block of memory.

Types of Array in java

There are two types of array.

o Single Dimensional Array


o Multidimensional Array

SYNTAX:
Datatype array name[size];
Example
int num[20];

Inheritance in Java
• Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
• It is an important part of OOPs (Object Oriented programming system).
• The idea behind inheritance in Java is that we can create new classes that are built upon
existing classes.
• When we inherit methods from an existing class, we can reuse methods and fields of
the parent class.
• However, we can add new methods and fields in your current class also.
What is Inheritance?
• Inheritance in Java enables a class to inherit properties and actions from another class,
called a superclass or parent class.
• A class derived from a superclass is called a subclass or child group.
• Through inheritance, a subclass can access members of its superclass (fields and
methods), enforce reuse rules, and encourage hierarchy.
• Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.

Why use inheritance in Java?

o For Method Overriding (so runtime polymorphism can be achieved).


o For Code Reusability.

Terms used in Inheritance

o Class: A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created.
o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
o Reusability: As the name specifies, reusability is a mechanism which facilitates you
to reuse the fields and methods of the existing class when you create a new class. You
can use the same fields and methods already defined in the previous class.
The syntax of Java Inheritance

1. class Subclass-name extends Superclass-name


2. {
3. //methods and fields
4. }

• The extends keyword indicates that we are making a new class that derives from an
existing class.
• The meaning of "extends" is to increase the functionality.
• In the terminology of Java, a class that is inherited is called a parent or superclass,
and the new class is called child or subclass.

Java Inheritance Example

• As displayed in the above figure, Programmer is the subclass and Employee is the
superclass.
• The relationship between the two classes is Programmer IS-A Employee.
• It means that Programmer is a type of Employee.

File Name: Programmer.java

1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. System.out.println("Programmer salary is:"+p.salary);
9. System.out.println("Bonus of Programmer is:"+p.bonus);
10.}
11.}

Programmer salary is:40000.0


Bonus of programmer is:10000

In the above example, Programmer object can access the field of own class as well as of
Employee class i.e. code reusability.

Types of Inheritance in Java


Java supports the following four types of inheritance:

o Single Inheritance
o Multi-level Inheritance
o Hierarchical Inheritance
o Hybrid Inheritance

On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only.
We will learn about interfaces later.
Note: Multiple inheritance is not supported in Java through class.
When one class inherits multiple classes, it is known as multiple inheritance. For Example:

Single Inheritance Example


When a class inherits another class, it is known as a single inheritance. In the example given
below, Dog class inherits the Animal class, so there is the single inheritance.

File: TestInheritance.java

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10.d.bark();
11.d.eat();
12.}}
Output:

barking...
eating...
Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see
in the example given below, BabyDog class inherits the Dog class which again inherits the
Animal class, so there is a multilevel inheritance.

File: TestInheritance2.java

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10.class TestInheritance2{
11.public static void main(String args[]){
12.BabyDog d=new BabyDog();
13.d.weep();
14.d.bark();
15.d.eat();
16.}}
Output:

weeping...
barking...
eating...
Hierarchical Inheritance Example
When two or more classes inherits a single class, it is known as hierarchical inheritance. In
the example given below, Dog and Cat classes inherits the Animal class, so there is
hierarchical inheritance.

File: TestInheritance3.java

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10.class TestInheritance3{
11.public static void main(String args[]){
12.Cat c=new Cat();
13.c.meow();
14.c.eat();
15.//c.bark();//C.T.Error
16.}}
Output:

meowing...
eating...

Note: Multiple inheritance is not supported in Java.

Single Inheritance

In single inheritance, a sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes it is also known as simple
inheritance.

In the above figure, Employee is a parent class and Executive is a child class. The Executive
class inherits all the properties of the Employee class.

Let's implement the single inheritance mechanism in a Java program.

Executive.java
1. class Employee
2. {
3. float salary=34534*12;
4. }
5. public class Executive extends Employee
6. {
7. float bonus=3000*6;
8. public static void main(String args[])
9. {
10.Executive obj=new Executive();
11.System.out.println("Total salary credited: "+obj.salary);
12.System.out.println("Bonus of six months: "+obj.bonus);
13.}
14.}
Output:

Total salary credited: 414408.0


Bonus of six months: 18000.0

Multi-level Inheritance

In multi-level inheritance, a class is derived from a class which is also derived from another
class is called multi-level inheritance. In simple words, we can say that a class that has more
than one parent class is called multi-level inheritance. Note that the classes must be at
different levels. Hence, there exists a single base class and single derived class but multiple
intermediate base classes.

In the above figure, the class Marks inherits the members or methods of the class Students.
The class Sports inherits the members of the class Marks. Therefore, the Student class is the
parent class of the class Marks and the class Marks is the parent of the class Sports. Hence,
the class Sports implicitly inherits the properties of the Student along with the class Marks.

Let's implement the multi-level inheritance mechanism in a Java program.

MultilevelInheritanceExample.java

1. //super class
2. class Student
3. {
4. int reg_no;
5. void getNo(int no)
6. {
7. reg_no=no;
8. }
9. void putNo()
10.{
11.System.out.println("registration number= "+reg_no);
12.}
13.}
14.//intermediate sub class
15.class Marks extends Student
16.{
17.float marks;
18.void getMarks(float m)
19.{
20.marks=m;
21.}
22.void putMarks()
23.{
24.System.out.println("marks= "+marks);
25.}
26.}
27.//derived class
28.class Sports extends Marks
29.{
30.float score;
31.void getScore(float scr)
32.{
33.score=scr;
34.}
35.void putScore()
36.{
37.System.out.println("score= "+score);
38.}
39.}
40.public class MultilevelInheritanceExample
41.{
42.public static void main(String args[])
43.{
44.Sports ob=new Sports();
45.ob.getNo(0987);
46.ob.putNo();
47.ob.getMarks(78);
48.ob.putMarks();
49.ob.getScore(68.7);
50.ob.putScore();
51.}
52.}
Output:

registration number= 0987


marks= 78.0
score= 68.7

Hierarchical Inheritance

If a number of classes are derived from a single base class, it is called hierarchical
inheritance.

In the above figure, the classes Science, Commerce, and Arts inherit a single parent class
named Student.

Let's implement the hierarchical inheritance mechanism in a Java program.

HierarchicalInheritanceExample.java

1. //parent class
2. class Student
3. {
4. public void methodStudent()
5. {
6. System.out.println("The method of the class Student invoked.");
7. }
8. }
9. class Science extends Student
10.{
11.public void methodScience()
12.{
13.System.out.println("The method of the class Science invoked.");
14.}
15.}
16.class Commerce extends Student
17.{
18.public void methodCommerce()
19.{
20.System.out.println("The method of the class Commerce invoked.");
21.}
22.}
23.class Arts extends Student
24.{
25.public void methodArts()
26.{
27.System.out.println("The method of the class Arts invoked.");
28.}
29.}
30.public class HierarchicalInheritanceExample
31.{
32.public static void main(String args[])
33.{
34.Science sci = new Science();
35.Commerce comm = new Commerce();
36.Arts art = new Arts();
37.//all the sub classes can access the method of super class
38.sci.methodStudent();
39.comm.methodStudent();
40.art.methodStudent();
41.}
42.}
Output:

The method of the class Student invoked.


The method of the class Student invoked.
The method of the class Student invoked.
Hybrid Inheritance

Hybrid means consist of more than one. Hybrid inheritance is the combination of two or
more types of inheritance.

In the above figure, GrandFather is a super class. The Father class inherits the properties of
the GrandFather class. Since Father and GrandFather represents single inheritance. Further,
the Father class is inherited by the Son and Daughter class. Thus, the Father becomes the
parent class for Son and Daughter. These classes represent the hierarchical inheritance.
Combinedly, it denotes the hybrid inheritance.

Let's implement the hybrid inheritance mechanism in a Java program.

Daughter.java

1. //parent class
2. class GrandFather
3. {
4. public void show()
5. {
6. System.out.println("I am grandfather.");
7. }
8. }
9. //inherits GrandFather properties
10.class Father extends GrandFather
11.{
12.public void show()
13.{
14.System.out.println("I am father.");
15.}
16.}
17.//inherits Father properties
18.class Son extends Father
19.{
20.public void show()
21.{
22.System.out.println("I am son.");
23.}
24.}
25.//inherits Father properties
26.public class Daughter extends Father
27.{
28.public void show()
29.{
30.System.out.println("I am a daughter.");
31.}
32.public static void main(String args[])
33.{
34.Daughter obj = new Daughter();
35.obj.show();
36.}
37.}
Output:

I am daughter.

Multiple Inheritance (not supported)

Java does not support multiple inheritances due to ambiguity. For example, consider the
following Java program.

Demo.java

1. class Wishes
2. {
3. void message()
4. {
5. System.out.println("Best of Luck!!");
6. }
7. }
8. class Birthday
9. {
10.void message()
11.{
12.System.out.println("Happy Birthday!!");
13.}
14.}
15.public class Demo extends Wishes, Birthday //considering a scenario
16.{
17.public static void main(String args[])
18.{
19.Demo obj=new Demo();
20.//can't decide which classes' message() method will be invoked
21.obj.message();
22.}
23.}

The above code gives error because the compiler cannot decide which message() method is
to be invoked.

Due to this reason, Java does not support multiple inheritances at the class level but can be
achieved through an interface.

You might also like