A
A
Andrey Titov2016-05-06 19:11:15
OOP
Andrey Titov, 2016-05-06 19:11:15

What is inherited in OOP?

Is the Class itself or its Interface inherited?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-05-06
@alexey-m-ukolov

The class is inherited , the interface is implemented .

interface I {}
class A {}
class B extends A implements I {}

A class is usually referred to as an object blueprint. An interface is a contract that an object must fulfill.
Take, for example, Bicycle , Truck and Motorcycle .
But each of these objects implements the Vehicle contract . If you have an object that is a Vehicle, you are always sure that you will get from point A to point B and at the same time it does not matter to you how many wheels this object has.
But if you have a Car contract , then you know that you can perform actions such as Open the Hood , Turn on the Wipers . The bike will not be able to implement such an interface. And the Truck will implement both contracts: if you want - move, if you want - open the hood.

V
VZVZ, 2016-05-06
@VZVZ

> Is the Class or its Interface inherited?
What does "its interface" mean?
An interface is a separate architectural unit, just like a class. Described by the "interface" keyword.
By default, a class does not have any interface (unless it implements one itself).
---
If a class inherits a class (in Java it is extends), then its methods, fields, properties are inherited. That is, the child class acquires all the members of the base class, and can still add something from itself (or override the inherited class). Methods are inherited along with their implementation, i.e. for the child class, they will work the same way as for the base class.
For example, let's take a GUI: the Button and Label classes inherit the base Control class, while the Left, Top, Width, Height properties inherited from Control work in the same way in both classes. At the same time, the implementation and headers of these methods are written only in Control.
If a class implements an interface (in Java it is implements), then it receives method headers from the interface, and it must implement them itself, and that's all. There are no method implementations in the interface, this is its main difference from the class.

int foo() // - это заголовок метода
{ 
// а здесь реализация (она же определение, тело...) метода (собственно код)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question