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
27
votes
5 answers

CodeIgniter 2: How to extend CI_Controller multiple times?

I have successfully extended the CI_Controller class by creating a MY_Controller.php which I have placed in the application/core directory. core/My_Controller.php looks something like this: class MY_Controller extends CI_Controller { function…
Camsoft
  • 11,718
  • 19
  • 83
  • 120
19
votes
1 answer

php anonymous class extends dynamic

anyone of you know how to get effect like in these code public function create(SomeInterface $obj) { $class = get_class($obj); return new class extends $class { //some magic here } } Obvious that code will not work in…
adeptofvoltron
  • 369
  • 1
  • 9
11
votes
5 answers

Custom Control in ASP.NET C#

I created a simple custom control that only inherits from the Literal control, and doesn't have any extensions yet, code is empty. Namespace: CustomControls Class name: Literal : System.Web.UI.WebControls.Literal Next thing I do is registering this…
geevee
  • 5,411
  • 5
  • 30
  • 48
8
votes
5 answers

codeigniter MY_Controller not found

i’m using the Codeigniter.2.1.3 for a website, so i need to extend the CI_Controller so i can add a method to be executed with all controllers so i did what’s in the user_guide: creating a file named MY_Controller.php in the application/core folder…
Yahya KACEM
  • 2,481
  • 7
  • 34
  • 61
6
votes
1 answer

How to extend Zend\Db\Sql\Select?

I am using MSSQL and want to implement the WITH function (as per Using ZF2, create a WITH statement?). To do so, I am extending the \Zend\Db\Sql\Select class adding the properties and methods required to add the WITH function. How do I now tell my…
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
6
votes
3 answers

Extending Ext.data.Store

I am trying to centralize my configuration of EXTJS stores within my application, however, I cannot seem to figure out how to make this happen. I am using ExtJS 4.1. I have a base store, which I want to hold all of the repetitive configuration…
Jim Eckels
  • 179
  • 1
  • 8
5
votes
1 answer

PHP child class cannot implement same interface parent class implements

Is it normal behavior that child class cannot implement same interface parent class implements? I got PHP v5.6 interface blueprint { public function implement_me(); } class one implements blueprint { public function implement_me() { …
Roman Toasov
  • 747
  • 11
  • 36
5
votes
1 answer

Extend TextBox control and change part of default style

I want to create a class library (dll file) of the WPF TextBox with extended functionality. But i want to change one part of the TextBox's default style (IsMouseOver property trigger). I created a new WPF user control library project, deleted the…
moonlander
  • 143
  • 2
  • 8
5
votes
1 answer

How can I extend a 3rd party Javascript object using typescript

I've been trying to figure out how to do this most of the after noon, and I'm either not getting something, or I'm approaching things wrong. I'm currently working on a project where I need to override an existing Javascript object for which I have…
shawty
  • 5,729
  • 2
  • 37
  • 71
5
votes
2 answers

Modify controller in Opencart without touching the core?

Is there a way for developers to modify controller and model methods in Opencart without having to touch the core files? Much like the way WP has the functions.php file where you can modify the guts of WP without worrying about future upgrades…
enchance
  • 29,075
  • 35
  • 87
  • 127
4
votes
3 answers

Why does overriding a method only occur after I implement an interface?

I've been reading through the book Working Effectively with Legacy Code and I've been playing around with the concept of overriding difficult to test methods in unit tests via the creation of a fake. I put together an example of what I thought would…
mezoid
  • 28,090
  • 37
  • 107
  • 148
3
votes
5 answers

Overload method with subclasses as parameters but have the method called as the superclass

I have an abstract class Animal, with two extending classes, Dog and Cat. public abstract class Animal { ... } public class Dog extends Animal { ... } public class Cat extends Animal { ... } In another class, I have an ArrayList animals…
Luke Neville
  • 43
  • 1
  • 5
3
votes
1 answer

Angular 2 inject all Inputs and Outputs into extended component

I am needing a way to automatically inject Inputs and Outputs into a wrapped component. The setup is a SimpleComponent which has a number of Inputs and Outputs. I want to wrap this element in another element and this has been achieved by creating an…
Carlton
  • 838
  • 6
  • 16
3
votes
1 answer

Cannot set/get the property of extended class in typescript

I have an issue related to setting or getting the value of the extended class in Typescript and angular 2. I have a class A with these properties: export class A { protected name: string; set setName(name: string) { this.name=…
PaladiN
  • 4,625
  • 8
  • 41
  • 66
3
votes
4 answers

c++ call constructor of extended class stored in base class variable without knowing extended class

I am currently programming something where I hit this problem: I have a Scene class and a MainScene class: class Scene{ Scene(); } class MainScene : public Scene{ MainScene(); } And what I want to do is keep track of a list of scenes like…
1
2 3 4 5 6 7