RUVIDEO
Поделитесь видео 🙏

inheritance and aggregation in Java

Inheritance in Java
======================================================
Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object.

Object Properties - Member Variables
Object behaviors - Member Functions

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.


Why use inheritance in java???
======================================================
1. For Method Overriding (so runtime polymorphism can be achieved).
2. For Code Reusability.


The syntax of Java Inheritance
======================================================
class SubClassName extends SuperClassName {
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from an existing class.
The meaning of "extends" is to increase the functionality.

superclass parent base class

subclass child derived class




Types of inheritance in java
======================================================
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.



Q) Why multiple inheritance is not supported in java?
======================================================
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes.
If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error
if you inherit 2 classes. So whether you have same method or different, there will be compile time error.


============================================================================================================

Ques: If we dont have IS-A relationsip can we re-use the code?
Ans: Yes - by using Aggregation(HAS-A) approach.

Advantage: If we dont have IS-A relationsip, we can re-use the code.


Aggregation in Java
======================================================
If a class have an entity reference, it is known as Aggregation.
Aggregation represents HAS-A relationship.

Consider a situation, Employee object contains many informations such as id, name, emailId etc.
It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.

class Employee {
int id;
String name;
Address address;//Address is a class
...
}
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.


Why use Aggregation?
=====================================
For Code Reusability.


When use Aggregation?
=====================================
Code reuse is also best achieved by aggregation when there is no is-a relationship.

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «inheritance and aggregation in Java», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.