UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Inheritance In Java

D
Deleted U.
20/06/2017 0 0

Inheritance:

The process of getting properties and behaviors from one class to another class is called inheritance.

  • Properties: Variables
  • Behaviors: Methods
  1. The main purpose of the inheritance is code extensibility whenever we are extending automatically the code is reused.
  2. In inheritance one class giving the properties and behavior and another class is taking the properties and behavior.
  3. Inheritance is also known as is-a relationship means two classes are belongs to the same hierarchy.
  4. By using extends keyword we are achieving inheritance concept.
  5. In the inheritance the person who is giving the properties is called parent the person who is taking the properties is called child.
  6. To reduce length of the code and redundancy of the code sun peoples introducing inheritance concept.

Types of inheritance:

Single inheritance:

  • Class A: Extending the properties from single parent class to B extends A single child class is called single inheritance.
  • Class B: Class A is Parent class, Class B is Child class.

1. Multilevel inheritance: Class A, B extends A, Class B one super and one sub class at each and every level. C extends B Class C.


2. Multiple inheritance:The process of getting properties and behaviors form more than one super class to the one child class. The multiple inheritance is not possible in the java language so one class can extends only one class at time it is not possible to extends more than one class at time.

  • Class B extends A - Possible
  • Class C extends B - Possible
  • Class C extends A, B - Not possible
  • Class A Class B
  • C extends A, B
  • Class C

3. Hierarchical inheritance: The process of getting properties and behaviors from one super class to the more than one sub classes is called hierarchical inheritance.

  • Class A
  • B extends A C extends A D extends A
  • Class B Class C Class D

4. Hybrid inheritance: Combination of any two inheritances is called as hybrid inheritance. If are taking the multilevel and hierarchical that combination is called hybrid inheritance.

  • Class A
  • B extends A
  • Class B
  • C extends B
  • Multilevel inheitance Class C
  • Hierarchical inheritance
  • D extends C E extends C
  • Class D class E
  • Before inheritance :-
  • Class A

{
Void m1();
Void m2();
};
class B
{
Void m1();
Void m2();
Void m3();
Void m4();
};
class C
{
Void m1();
Void m2();
Void m3();
Void m4();
Void m5();
Void m6();
}
------------------------------------------------------------------------------------------------------------
After inheritance:
class A
{
Void m1() parent class/
Void m2() super class/
Void m3() base class
}
class B extends A
{ child class/
Void m4() sub class
Void m5() derived class
}
Class C extends B
{
Void m6();
}
Note 1:
It is possible to create objects for both parent and child classes.
a. If we are creating object for parent class it is possible to call only parent specific methods.
A a=new A();
a.m1();
a.m2();
a.m3();

b. if we are creating object for child class it is possible to call parent specific and child specific.
B b=new B();
b.m1();
b.m2();
b.m3();
b.m4();
b.m5();

c. if we are creating object for child class it is possible to call parent specific methods and child specific methods.
C c=new C();
c.m1();
c.m2();
c.m3();
c.m4();
c.m5();
c.m6();

Ex:
class Parent
{
public void m1()
{
System.out.println("m1-method");
}
public void m2()
{
System.out.println("m2-method");
}
}
------------------------------------------------------------------------------------------------------
class Child extends Parent
{
public void m3()
{
System.out.println("m3-method");
}
public static void main(String[] args)
{
Child c=new Child();
c.m1();
c.m2();
c.m3();
Parent p=new Parent();
p.m1();
p.m2();
}
}

Note:

1. Every class in the java programming is a child class of object.
2. The root class for all java classes is Object class.
3. The default package in the java programming is java.lang package.
Both declaration are same:-
class Test
{
};
class String
{
};
class Test extends Object
{
};
class String extends Object
{
};

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

how to write the answer
always highlight the key word of writing any answer without fail,also draw simple figure each and every answer related question accordingly.

For B.Tech Students: Applied Chemistry - First Year.
The dispersity index, or PDI/ polydispersity index is measure of the distributon of molecular mass in a given polymer sample. Lets try an example:

Android : Application Launch time improvements.
For any standard android application, below 3 components play important roles to show 1st interface to user, so that he/she can interact with the app. 1. Custom Application class : Intialize the components...

Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video

Easy way to remember Java keyword.
ACCESS MODIFIER ACCESS SPECIFIER abstract, assert, const, final, native, static, strictfp, super, synchronized, this, transient, void, volatile public, private, protected, default DATA...

Rohit Deshbhratar

1 0
0
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more