จากตัวอย่างข้างต้น Person เป็น base class และ Employee เป็น derived class ซึ่ง constructor ของคลาส Person มีพารามิเตอร์ คือ name และ age ซึ่งเป็นกำหนดค่าเริ่มต้นให้กับฟิลล์ Name และ Age คลาสEmployee สืบทอด คลาส Person มาและประกาศ constructor ที่มีพารามิเตอร์ ได้แก่ name , age และ employeeId ซึ่ง มีการเรียก constructor ของ Person โดยใช้คำว่า :base(name, age) เพื่อดึง พารามิเตอร์ name และ age มาใช้นั่นเอง
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee("Somying", 25, 12345);
Console.WriteLine($"Name: {emp.Name}, Age: {emp.Age},
EmployeeId: {emp.EmployeeId}");
}
}
Name: Somying, Age: 25, EmployeeId: 12345
C#
using System;
class Tank {
double t_radius;
double t_height;
public double Radius{
get { return t_radius; }
set {t_radius = value < 0 ? -value : value;}
}
public double Height{
get { return t_height; }
set { t_height = value < 0 ? -value : value; }
}
public void DisplayDimension(){
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
class AreaOfTank : Tank {
string Color;
// Constructor
public AreaOfTank(string c, double r, double h){
// from base class
Radius = r;
Height = h;
// from derived class
Color = c;
}
public double Area(){
return 2 * 3.14 * Radius * Height;
}
public void DisplayColor(){
Console.WriteLine("The Color of tank is " + Color);
}
}
class GFG {
static void Main(){
AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}
using System;
class Tank {
double t_radius;
double t_height;
// Constructor for Tank
public Tank(double r, double h){
Radius = r;
Height = h;
}
public double Radius{
get { return t_radius; }
set { t_radius = value < 0 ? -value : value; }
}
public double Height{
get { return t_height; }
set { t_height = value < 0 ? -value : value; }
}
public void DisplayDimension(){
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
class AreaOfTank : Tank {
string Color;
// Call the Constructor of the
// base class, i.e Tank
// Using base keyword
public AreaOfTank(string c, double r, double h) : base(r, h){
Color = c;
}
public double Area(){
return 2 * 3.14 * Radius * Height;
}
public void DisplayColor(){
Console.WriteLine("The Color of tank is " + Color);
}
}
class GFG {
static void Main(){
AreaOfTank t1 = new AreaOfTank("Brown", 4.0, 8.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}
java
// super keyword with constructor
// superclass Person
class Person {
Person()
{
System.out.println("Person class Constructor");
}
}
// subclass Student extending the Person class
class Student extends Person {
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
// Driver Program
class Test {
public static void main(String[] args)
{
Student s = new Student();
}
}
Person class Constructor
Student class Constructor
C++
#include <iostream>
using namespace std;
class Base {
int x;
public:
// Default constructor
Base() {
cout << "Base class default constructor\n";
}
// Parameterized constructor
Base(int x) {
this->x = x;
cout << "Base class parameterized constructor\n";
}
};
class Derived : public Base {
int y;
public:
// Default constructor
Derived() {
cout << "Derived class default constructor\n";
}
// Parameterized constructor
Derived(int i) : Base(i) {
cout << "Derived class parameterized constructor\n";
}
};
int main() {
Base b; // Construct base class object
Derived d1; // Construct derived class object with default constructor
Derived d2(10); // Construct derived class object with parameterized constructor
}
Base class default constructor
Base class default constructor
Derived class default constructor
Base class parameterized constructor
Derived class parameterized constructor
python
class Emp():
def __init__(self, id, name, Add):
self.id = id
self.name = name
self.Add = Add
# Class freelancer inherits EMP
class Freelance(Emp):
def __init__(self, id, name, Add, Emails):
super().__init__(id, name, Add)
self.Emails = Emails
Emp_1 = Freelance(103, "Suraj kr gupta", "Noida" , "KKK@gmails")
print('The ID is:', Emp_1.id)
print('The Name is:', Emp_1.name)
print('The Address is:', Emp_1.Add)
print('The Emails is:', Emp_1.Emails)
The ID is: 103
The Name is: Suraj kr gupta
The Address is: Noida
The Emails is: KKK@gmails