filmov
tv
Example of Inheritance in OOP #objectorientedprogramming #programming #coding
![preview_player](https://i.ytimg.com/vi/sjIUU-3oTzo/maxresdefault.jpg)
Показать описание
🔻 The real code example 🔻
In Java it would look like that:
We have a class for a generic listing with some properties (and methods, which I omitted here for simplicity)
class GenericListing {
private String title;
private String description;
private double price;
// This is a constructor method - blueprint to populate above properties with values
public GenericListing(String title, String description, double price) {
}
}
The class that we are extending from is usually called Parent class, or superclass, or base class. In our case it is GenericListing.
The class which extends it is called Child class, subclass or derived class. In our case it is DigitalListing.
To specify that we want to inherit properties and methods of GerericListing class we want to use keyword extends.
class DigitalListing extends GenericListing {
private String url;
public DigitalListing(String title, String description, double price, String url) {
//To populate values of a class we are extending, we need to call its constructor method. For that we can use super keyword
super(title, description, price);
}
}
DigitalListing now has all the properties that GenericListing has, and its own property url.
Side note: here is the same example in Python
class GenericListing:
def __init__(self, title, description, price):
class DigitalListing(GenericListing):
def __init__(self, title, description, price, url):
super().__init__(title, description, price)
What are the benefits?
We reduced the amount of code we need to write. This helps us to put common things into one place (parent class) and reuse them instead of implementing the same thing again in the Child class.
❓❓❓ Do you have any other inheritance related questions? Ask below 👇
#nikavscode #programmingtips #codingcommunity #oopprogramming #inheritanceinjava #inheritanceinpython #developerslife #learnprogrammingfromphone
In Java it would look like that:
We have a class for a generic listing with some properties (and methods, which I omitted here for simplicity)
class GenericListing {
private String title;
private String description;
private double price;
// This is a constructor method - blueprint to populate above properties with values
public GenericListing(String title, String description, double price) {
}
}
The class that we are extending from is usually called Parent class, or superclass, or base class. In our case it is GenericListing.
The class which extends it is called Child class, subclass or derived class. In our case it is DigitalListing.
To specify that we want to inherit properties and methods of GerericListing class we want to use keyword extends.
class DigitalListing extends GenericListing {
private String url;
public DigitalListing(String title, String description, double price, String url) {
//To populate values of a class we are extending, we need to call its constructor method. For that we can use super keyword
super(title, description, price);
}
}
DigitalListing now has all the properties that GenericListing has, and its own property url.
Side note: here is the same example in Python
class GenericListing:
def __init__(self, title, description, price):
class DigitalListing(GenericListing):
def __init__(self, title, description, price, url):
super().__init__(title, description, price)
What are the benefits?
We reduced the amount of code we need to write. This helps us to put common things into one place (parent class) and reuse them instead of implementing the same thing again in the Child class.
❓❓❓ Do you have any other inheritance related questions? Ask below 👇
#nikavscode #programmingtips #codingcommunity #oopprogramming #inheritanceinjava #inheritanceinpython #developerslife #learnprogrammingfromphone