Questions tagged [extending-classes]

Method extending, in object oriented programming, is a language feature that allows a subclass or child class to inherit all the methods and properties of the extended class.

You can extend a class to provide a more specialized behavior.

A class that extends another class inherits all the methods and properties of the extended class. In addition, the extending class can override the existing methods. Overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behaviour of a particular method is different based on the object you're calling it on. This is referred to as polymorphism.

A class extends another class using the extends keyword in the class definition. A class can only extend one other class.

Example in PHP

class Hello {
    public function message() {
        return "Hello";
    }
}

class HelloWorld extends Hello {
    public function message() {
        return parent::message() . " World";
    }
}

$class = new HellowWord();
echo $class->message(); //"Hello World"

In this example we the class Hello, and the function message.

Related tags:

100 questions
0
votes
5 answers

C# add to class in another DLL

...is it possible? For example, can I add this simple function... public static double Deg2Rad(double degrees) { return Math.PI / 180 * degrees; } ...to the Convert class? So (using the default..."usings") you can call double radians =…
Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
0
votes
0 answers

Overriding Methods in React components - an antipattern, but how do I do it?

Believe me when I say that if I had any other choice, I would take it, but I am at my wits end. I'm working for a big corporation which already has a whole library dedicated to authorization. And the authorization works, for the most part,…
Brian Boyko
  • 613
  • 6
  • 15
0
votes
1 answer

How to extend a laravel package within laravel 5.8 application?

I am new to Laravel and would appreciate any help on describing how a Laravel package residing in vendor folder can be extended and wont get affected if I update the package.
0
votes
1 answer

How can I inherit from the pandas Series class in order to simplify it for a subset of Series types?

I want to create a new class that allows me to create pandas Series objects that are restricted so the user only needs to enter a start date and number of periods to initialize the series. I also want to be able to replace values given a date within…
JasonArg123
  • 188
  • 1
  • 1
  • 14
0
votes
2 answers

Extending RequiredFieldValidator

Hello I'm trying to extend the requirefieldvalidator to take a new property and validate regularexpressions also. I know I can use the RegularExpression control but then i need 2 controls so i want to eliminate it so i only have to use two controls.…
Jan Johansen
  • 1,999
  • 6
  • 30
  • 43
0
votes
1 answer

Extending a class, delegating operators and assignment?

In a nutshell... in my app we use boost::filesystem::path a whole lot. It mostly works very well, EXCEPT if somebody decides to be cute and refer to a non-unicode filename in windows (say, for some reason I cannot fathom, somebody has a Shift-JIS …
0
votes
3 answers

Are there any reasons not to extend std::set to add a subscript operator?

I'm using std::set to store unique instances of a class. std::set does not have an overloaded subscript operator so you can't do set[0] for example. I found a way to do it: auto myClass = *std::next(set.begin(), index); However, I found it…
0
votes
2 answers

Ruby on Rails extending a gem's class and changing its inheriting class

I have a web app built on Ruby on Rails. In this app obviously use different Gems. Iv come to a point where i want to extend some specific gem's classes, and add methods to it. Now i have a use case where i want to extend a gem's class, but instead…
0
votes
1 answer

yii2 custom field type

I am using Yii2 and I want to create custom active form field type. For example creatig a text input form field is happending like this: $form = ActiveForm::begin(); $form->field($model, 'attribute_name')->textInput(['maxlength' => true]) I want…
Chris
  • 3
  • 3
0
votes
1 answer

Laravel Spark Token Visibility

I am trying to allow users to see their tokens. Laravel\Spark\Token looks partially like this:
Bryan
  • 17,201
  • 24
  • 97
  • 123
0
votes
0 answers

C# Override a class to allow custom logic

I am working with the IdentityUser class [Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser], and would like to extend (or override) the IdentityUser class to allow null(able) claim values, but am having a hard time trying to figure out…
CodingRiot
  • 135
  • 1
  • 3
  • 7
0
votes
1 answer

Extending Application from a library project

I know how to extend the Application class from my main project, but currently i am extending also from a library project (to uncouple code), so i perform in this way: 1- public class MyMainApplication extends LibraryApp {... 2- public class…
Billyjoker
  • 729
  • 1
  • 10
  • 31
0
votes
1 answer

Is it good practice to extend a class by using a method?

I've seen this question and I know how to extend a class. However, is it good practice to do that with a method? Is it safe to use this: function extendClass(child, parent) { child.prototype = Object.create(parent.prototype); …
dodov
  • 5,206
  • 3
  • 34
  • 65
0
votes
3 answers

ZF2 change extends class

The answer to my question ZF2 FormInput to show error class on validation fail is to create my own form view helper, overriding the render function. While this works beautifully for elements being rendered using forminput, it doesn't help on…
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
0
votes
1 answer

Extending a parent and using parent render method react native

This is a general question as I've not seen anything like this anywhere. So if I have a parent component like the examples below and I was to render the children of the child in the parent how could I do this if its possible (other than making a…
TheMan68
  • 1,429
  • 6
  • 26
  • 48