C++: Classes and objects

This entry is part 19 of 61 in the series C++

A class contains data and functions. A class could be understood as the object in an object-oriented program.

When we are defining a class, we are actually defining an object.

In object-oriented concept, or in any other object-oriented programming languages, we would usually call a function a method of the class.

As was mentioned, a class contains data members and methods.  The reason that we called them methods is mainly because it is through the methods that we act on the data in the class.

Data members could just be any integer or string which is similar to a variable in a procedural language.



Java: A simpler abstract class example

This entry is part 49 of 54 in the series Learn Java

An abstract class provides a contract between a service provider and its clients, but at the same time an abstract class can provide partial implementation.

Methods that must be explicitly overridden can be declared abstract.

package javaapplication41;

public class MainMusic {

    public abstract class Music {

        public abstract int live();            

    static class Rock extends Music {

        protected int ls;                              

        public Rock(int l) {
            this.ls = l;

        public int yearly() {
            return ls * 52;

        public int live() {
            return ls;

    public static void main(String[] args) {

        Rock r = new Rock(5);



Java: Access to classes and members

This entry is part 45 of 54 in the series Learn Java

By default, top-level classes are accessible within the package in which they are defined. However, if a top-level class is declared public, it is accessible everywhere.

The members of a class are always accessible within the body of the class. By default, members are also accessible throughout the package in which the class is defined.

This default level of access is often called package access. It is only one of four possible levels of access. The other three levels are defined by the public, protected, and private modifiers.

package javaapplication40;

public class Music {

    private String artiste;
    protected String rockband;

    public void live() {
        this.artiste = "Lady Gaga";

    public void record() {
        this.rockband = "Nirvana";

    public static void main(String[] args) {

        Music m = new Music();



Java: Class

This entry is part 20 of 54 in the series Learn Java

A class is a template used to create objects. They are made up of members, the main two of which are fields and methods. Fields are variables that hold the state of the object, while methods define what the object can do.

An object is also called an instance. The object will contain its own set of fields, which can hold values that are different to those of other instances of the class.

package javaapplication19;

public class JavaApplication19 {

    static class MyRectangle {

        int x, y;

        int getArea() {
            return x * y;

    public static void main(String[] args) {
        MyRectangle r = new MyRectangle();      
        int area = r.getArea();


C#: Designing a class

This entry is part 21 of 71 in the series C#

Say we have a friend who has developed a class called Swap.

From the brief documentation given by our friend, we know that there is a method that will swap 2 strings and our friend has also provided us a method to display the strings.

The example below will demonstrate how we can make use of the Swap class to swap 2 strings.

using System;

namespace SwapSpace
    class Swap
        string lhs;
        string rhs;

        public Swap(string _lhs, string _rhs)
            lhs = _lhs;
            rhs = _rhs;

        public void swap(string _lhs, string _rhs)
            string temp;
            temp = _lhs;
            lhs = rhs;
            rhs = temp;

        public void display()
            Console.WriteLine(lhs + " " + rhs);

    class Program
        static void Main(string[] args)
            string l = "left";
            string r = "right";

            Swap swapStr = new Swap(l,r);
            swapStr.swap(l, r);