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

Java Collection
Collection: The collection provides an architecture like a list where we can store and manipulate the group of items. Through Collections, we can achieve operations such as searching, sorting, insertion,...

Vinaykumar D.

0 0
0

How do i get best Campus / Off Campus Placement?
Companies are looking for Skilled Freshers. So build your technical skills while doing MCA / BTech / BCA / BSc (IT or CS) into below areas- 1. Strong your programming & debugging skills ...

Access Specifiers รข?? Private, Protected, Public, Package-private
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. The following table shows the access to members permitted by each modifier. Access...

Skills of the Future
We are aware that Automation has entered every walk of life. We have to accept that it eliminates lower level jobs. Hence we have to adapt and acquire new skills. Broadly it can be classifed into five...

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 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