Inheritance

Inheritance
Inheritance is an object-oriented programming technique that lets you reuse code of existing objects, creating a child class from the existing object. Subsequent repetitions of this creates what's known as a hierarchy. The child classes become a modular derivative class and the parent class creates a common interface and foundation for functionality between all child classes. A child class can also override the methods of the parent class, effectively replacing its contents. The modifiers put on objects in a class also come into play when accessing data from a child class. If the modifier on the data in the parent class is private, the subclass cannot access the data. If the modifier is protected, the subclass can access it but outside the class it acts like a private data type. If its public its free to edit it as well.

In Java
In Java inheritance is a common occurrence in large applications. A child class can be created by adding 'extends SomeParentClass' when creating the class.

code format="java" public class Main{ class Parent{ //parent class public int x; //can be accessed anywhere private int y; //can only be accessed from parent protected int z; //can only be accessed from parent and children public Parent{ //constructor x = 0; y = 0; z = 0; }       public void funct{ //basic function System.out.println("Parent"); }   }    class ChildOne extends Parent{ //extending the parent public ChildOne{ super; //calls the parent's contructor x = 1; z = 1; }       @Override public void funct{ //overrides the parent's method System.out.println("ChildOne"); }   }    class ChildTwo extends Parent{ //extending the parent public ChildTwo{ super; //calls the parent's method x = 2; z = 2; }       @Override public void funct{ //overrides the parent's method System.out.println("ChildTwo"); }   }    class ChildOfChildOne extends ChildOne{ //extending ChildOne public ChildTwo{ super; //calls ChildOne's constructor which in turn calls the parent z = 3; }       @Override public void funct{ //overrides the parent's method System.out.println("ChildOfChildOne"); }   }

public static void main(String[] args){ Parent var; var = new ChildOne; var.funct;

var = new Parent; var.funct;

var = new ChildOfChildOne; var.funct; } } code code Output: ChildOne Parent ChildOfChildOne code

In C++
Creating a child class in C++ is similar to Java, but remember we're creating a child CLASS, there is no such thing as a sub-struct. If you want to override a function from the parent class the function in the parent must be 'virtual'.

code format="cpp"
 * 1) include

class Person{ //parent class public: Person(int age); //constructor void setAge(int years); int getAge; char *getName; virtual char *toString{return "?????";} //virtual function char *name; private: int age; }; class Employee: public Person{ //child class public: Employee(char *name, int age, float monthly); //constructor float getCost; virtual char *toString{return "Employee";} //virtual private: float monthly; }; class Customer: public Person{ //child class public: Customer(char *name, int age, float annual); //contructor float getCost; virtual char *toString{return "Customer";} //virtual private: float annual; };

//Person methods Person::Person(int age){ this->age = age; } void Person::setAge(int years){ age = years; } int Person::getAge{ return age; } char *Person::getName{ return name; }

//Employee methods Employee::Employee(char *name, int age, float monthly) :Person(age){ //calls the parent constructor this->monthly = monthly; this->name = name; } float Employee::getCost{ return monthly; }

//Customer methods Customer::Customer(char *name, int age, float annual) :Person(age){ //calls the parent constructor this->annual = annual; this->name = name; } float Customer::getCost{ return annual; }

//main int main{ Employee tyler("Tyler Brazill", 18, 15000); Customer karson("Karson Kimbrel", 19, 5555);

printf("%s: %s's age is %d.\n", tyler.toString, tyler.getName, tyler.getAge); printf("%s: %s's age is %d.\n", karson.toString, karson.getName, karson.getAge); return 0; } code code Output: Employee: Tyler Brazill's age is 18. Customer: Karson Kimbrel's age is 19. code