Inheritance

Class inheritance in programming allows a new class (subclass) to inherit attributes and behaviors from an existing class (superclass), enabling code reuse and promoting a hierarchical structure in object-oriented programming. Subclasses can extend or modify the functionality of their superclass, while inheriting its properties. This mechanism facilitates the creation of more specialized and abstracted classes that share common characteristics. For example, consider a User class as the superclass and an Admin class as a subclass. The User class might contain attributes like username and email, as well as methods for authentication. The Admin class, inheriting from User, could have additional attributes and methods specific to administrative tasks, such as managing user accounts or permissions. This inheritance structure enables code reuse, as common functionality defined in the User class can be inherited and extended in the Admin class, promoting modularity and reducing redundancy.

Select Languages

Examples

C

// No Native Support or Implementation

C#

class User {
  protected string username;
  protected string password;
  
  public User(
    string Username,
    string Password
  ) {
    username = Username;
    password = Password;  
  }
}

class Admin: User {
  protected string id;
  
  public Admin(
    string username,
    string password,
    string Id
  ): base(username, password) {
    id = Id;
  }
  
  public string getUsername() {
    return username;
  }
  
  public string getAdminID() {
    return id;
  }
}

public class Test {
  public static void Main(string[] args) {
     Admin newAdmin = new Admin(
      "alpha0",
      "pw1",
      "1FE"
    );

    Console.WriteLine(
      newAdmin.getUsername()
    );

    Console.WriteLine(
      newAdmin.getAdminID()
    );
  }
}

C++

#include <iostream>
using namespace std;

class User {
  protected:
    string username;
    string password;
  public:
    void setUser(string x, string y) {
      username = x;
      password = y;
    }
};

class Admin: public User {
  protected: 
    string id;
  public:
    void setAdmin(string x) {
      id = x;
    }
    string getUsername() {
      return username;
    }
    string getAdminID() {
      return id;
    }
};

Admin newAdmin;
newAdmin.setUser("alpha0", "pw1");
newAdmin.setAdmin("1FE");
cout << newAdmin.getUsername() << "\n";
cout << newAdmin.getAdminID() << "\n";

Go

// No Native Support or Implementation

Java

class User {
  protected String username;
  protected String password;
  
  public User(
    String username,
    String password
  ) {
    this.username = username;
    this.password = password;  
  }
}

class Admin extends User {
  protected String Id;
  
  public Admin(
    String username,
    String password,
    String id
  ) {
    super(username, password);
    this.Id = id;
  }
  
  public String getUsername() {
    return this.username;
  }
  
  public String getAdminID() {
    return this.Id;
  }
}

class Test {
  public static void main(String[] args) {
    Admin newAdmin = new Admin(
      "alpha0",
      "pw1",
      "1FE"
    );
    System.out.println(
      newAdmin.getUsername()
    );
    System.out.println(
      newAdmin.getAdminID()
    );
  }
}

JavaScript

class User {
  username;
  password;

  constructor(username, password) {
    this.username = username;
    this.password = password;
  }
}

class Admin extends User {
  constructor(username, password, id) {
    super(username, password);
    this.id = id;
  }
  
  getAdminID() {
    return this.id
  }
  
  getUsername() {
    return this.username
  }
}

const newAdmin = new Admin(
  "alpha0",
  "pw1",
  "1FE"
)

console.log(newAdmin.getUsername());
console.log(newAdmin.getAdminID());

Kotlin

open class User(
  Username:String,
  Password:String
) {
  var username:String = Username
  var password:String = Password
  open fun fetchUsername():String {
    return username
  }
}

class Admin(
  username:String,
  password:String,
  Id:String
): User(
  username, password
) {
  var id:String = Id

  fun getAdminID():String {
    return id
  }
}

val user = Admin("alpha0", "pw1", "1FE")
println(user.fetchUsername())
println(user.getAdminID())

MatLab

classdef User
  properties
    username
    password
  end
end

classdef Admin < User
  properties
    id
  end
end

// command line
newAdmin = Admin;
newAdmin.id = "1FE";
newAdmin.username = "alpha0";
newAdmin.password = "pw1";

disp(newAdmin.username);
disp(newAdmin.id);

PHP

class User {
  protected $username;
  protected $password;
  
  public function __construct(
    $username, $password
  ) {
    $this->username = $username;
    $this->password = $password;
  }
}

class Admin extends User {
  protected $id;
 
  public function __construct(
    $username, $password, $id
  ) {
    $this->username = $username;
    $this->password = $password;
    $this->id = $id;
  }
  
  public function getUsername() {
    return $this->username ."\n";
  }
  
  public function getAdminID() {
    return $this->id;
  }
}


$newAdmin = new Admin(
  "alpha0",
  "pw1",
  "1FE"
);

echo $newAdmin->getUsername();
echo $newAdmin->getAdminID();

Python

class User:
  def __init__ (self, username, password):
    self.username = username
    self.password = password
    
class Admin(User):
  def __init__ (self, id, password, username):
    super().__init__(username, password)
    self.id = id
  def getUsername(self):
    return f"{self.username}"
  def getAdminID(self):
    return self.id
    
newAdmin = Admin('alpha 0', 'pw1', '1FE')
print(newAdmin.getUsername())
print(newAdmin.getAdminID())

R

User <- setRefClass("User",
  fields = list(
    username = "character",
    password = "character"
  )
)

Admin <- setRefClass("Admin",
  fields = list(
    id = "character"
  ),
  contains = "User"
)

newAdmin <- Admin(
  username = "alpha0",
  password = "pw1",
  id = "1FE"
)

print(newAdmin$username)
print(newAdmin$id)

Ruby

class User
  def initialize(username, password)
    @username = username
    @password = password
  end
end

class Admin < User 
  def initialize(username, password, id)
    super(username, password)
    @id = id
  end
  
  def getUsername()
    return @username
  end
  
  def getAdminID()
    return @id
  end
end

newAdmin = Admin.new("alpha0", "pw1", "1FE")

puts newAdmin.getUsername()
puts newAdmin.getAdminID()

Rust

// No Native Support or Implementation

Scala

class User(
  Username: String,
  Password: String
) {};

class Admin(
  Username: String, 
  Password: String,
  Id: String
) extends User(
  Username,
  Password
) {
  val username = Username;
  val password = Password;
  val id = Id;
}

val newAdmin = new Admin(
  "alpha0",
  "pw1",
  "1FE"
);

println(newAdmin.username);
println(newAdmin.id);

Swift

class User {
  var username:String;
  var password:String;
  
  init(username: String, password: String) {
    self.username = username
    self.password = password
  }
}

class Admin : User {
  var id:String;

  init(
    username: String, 
    password: String, 
    id:String) 
  {
    self.id = id
    super.init(
      username: username,
      password: password
    )
  }
  
  func fetchUsername() -> String {
    return self.username
  }

  func getAdminID() -> String  {
    return self.id
  }
}

var newAdmin = Admin(
  username: "alpha0",
  password: "pw1",
  id: "1FE"
)

print(newAdmin.fetchUsername())
print(newAdmin.getAdminID())

TypeScript

class User {
  private username:string;
  private password:string;

  constructor(
    username:string,
    password:string
  ) {
    this.username = username;
    this.password = password;
  }
  
  getUsername() {
    return this.username;
  }
}

class Admin extends User {
  id:string;
  constructor(
    username:string, 
    password:string, 
    id:string 
  ) {
    super(username, password);
    this.id = id;
  }
  
  getAdminID() {
    return this.id
  }
}

const newAdmin = new Admin(
  "alpha0",
  "pw1",
  "1FE"
)

console.log(newAdmin.getUsername());
console.log(newAdmin.getAdminID());

Copyright 2025. All Rights Reserved. IronCodeMan.