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
0 answers

PHP static property with extending class overwritten

Please, could you help me a bit with my problem? I have class called Translateable and then clasess Article and Banner, which extend this class. Problem occurs, when I do this: $article = (new Article)->find(15); $banner = (new…
Jan Kožušník
  • 683
  • 3
  • 16
  • 30
0
votes
4 answers

MyApplication not assignable to Activity

I am currently attempting to make a global variable activity. I have followed the following instructions (Android global variable) in order to set the Activity up. However, the problem comes when I attempt to edit the android:name attribute. When I…
Mildwood
  • 349
  • 2
  • 13
0
votes
1 answer

Woocommerce - Try to merge product data with data from custom tables

i have several custom tables and i'm using advanced custom fields (acf) with twig. These fields stored in the custom tables. for example: custom_table1, custom_table2, custom_table1_table2_relation and more of this. They can be edited in the admin…
jamie
  • 47
  • 1
  • 8
0
votes
1 answer

Extending a package model

I have installed Cmgmyr\Messenger, however, I need to extend the Thread model and potentially a couple of the others as my users table does not contain a name field. The method I need to extend is: /** * Generates a string of participant…
ChrisBratherton
  • 1,540
  • 6
  • 26
  • 63
0
votes
0 answers

Laravel 5 - Extending a Abstract Provider

I am trying to extend Laravel\Socialite\Two\AbstractProvider , and I am unsure what is the best / proper way to do this in Laravel. Reading the documentation has lead me to believe I should be doing this in a service provider. Any help would be…
LukePOLO
  • 1,110
  • 3
  • 13
  • 28
0
votes
2 answers

Extending mysqli and using multiple classes

I'm new to PHP oop stuff. I'm trying to create class database and call other classes from it. Am I doing it the right way? class database: class database extends mysqli { private $classes = array(); public function __construct()…
Mikk
  • 2,209
  • 3
  • 32
  • 44
0
votes
1 answer

Why doesn't my setter warn that the property cannot be set?

In class a I have a setter defined. In class b, which extends class a, there is a private variable that the class a will therefore not be able to see. The setter in class a in this code will never set the variable test to a value different than the…
Digital Ninja
  • 3,415
  • 5
  • 26
  • 51
0
votes
1 answer

Initialize extended class through base class factory in Java

It's my first question here so please don't be angry at me if something wrong. Here is the problem: We have class Node. It's a base class for class Item. So basically: public class Item extends Node Ok, still everything is fine. But we also have…
Serj.by
  • 534
  • 5
  • 17
0
votes
1 answer

PHP Bug in threaded environment assigning value to parent static member in same thread?

I have this issue where I wonder if it is a PHP bug or intentional behaviour. If I assign in the child class a MySQL resource to a parent member then the value gets lost when running as a thread. This code does not run - the MySQL resource should be…
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
0
votes
1 answer

(Java, Bukkit) Using extended class methods don't work (Noob oop)

I have one class named 'Regions', which make region and check if coordinates are in it. Then I have other class 'ArrowTower' which extends 'Region'. Class 'ArrowTower' creates towers. All day I was experimenting with no results. What I want to do is…
Anat0m
  • 37
  • 6
0
votes
2 answers

Updating a static variable when extending a Python class

I am writing an extension to a library and I would like to register my new function with a dictionary of function pointers in the base class. My understanding is that the dictionary, since it is at the uppermost scope within a class, is static and…
tlnagy
  • 3,274
  • 4
  • 24
  • 37
0
votes
2 answers

Setting Custom Values on Extended class

Im not sure if it is possible. I am running into a unique issue dealing with a clients api. I am needing to extend a class and add a bool property that does not exist in the base class. below is an example of what I am trying to accomplish. public…
Cgdynamic
  • 127
  • 8
0
votes
1 answer

Extending a generic model extending from ActiveRecord::Base

I have a generic Service model which contains a few generic functions including a few to be overridden by 'actual services'. service.rb class Service < ActiveRecord::Base def get_line_items raise "Not Implemented" end end I then have a…
bdx
  • 3,316
  • 4
  • 32
  • 65
0
votes
5 answers

In C++ can you extend a parameterized base class with different parameter value in the child class?

In all the languages that I understand this is not possible but someone was telling me it was possible in C++ but I have a hard time believing it. Essentially when you parameterize a class you are creating a unique class in the compilation stage…
stephenmm
  • 2,640
  • 3
  • 30
  • 48
0
votes
3 answers

Is it possible to set a Variable with a Type which is a superclass uf a class in C#?

More Informations: EDIT: better sample: I have a class in a Libary called UserAccount, its abstact. then i have some functionality like this in the library: class UserAccountService { public static UserAccount CreateUserAccount(String username,…
user2838883