Java: Inheriting a class with members

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

Let’s look at an inheritance example with members.

The class LibraryBook is a child class of a super class Book. Here, we say LibraryBook inherits states and behaviors of objects of Book class such as isbn, author, title and many others.

Apart from deriving from Base class, LibraryBook can have its own data members such as number of copies and member functions representing states and behaviors of objects.

package javaapplication31;

public class BookClass {

    static class Book {

        int isbn;
        String author;
        String title;

        public void init(int isbn, String author, String title) {
            this.isbn = isbn; // this.isbn refers to instance variable,
            // isbn refers to local variable
   = author;
            this.title = title;

    static public class LibraryBook extends Book {

        int nCopies = 10; // number of copies of the book

        public void show() {
            System.out.println("isbn = " + isbn + "\nauthor = " + author + "\ntitle = " + title + "\ncopies = " + nCopies);

    public static void main(String[] args) {

        LibraryBook lb = new LibraryBook();
        lb.init(4321, "Jeniffer", "Data Structures");;




C#: Inheritance or specialization

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

As mentioned in an earlier post, inheritance is one of the core ideas of Object-Oriented programming.

As compared to C++ and Java, inheritance in C# is much more refined.  For example, in C# a class can only extend one other class. This is unlike C++ where multiple inheritance is allowed.

The benefits of inheritance are obvious. Inheritance gives you the opportunity to add some functionality that does not exist in the original class.

It also gives you the chance to change the behaviors of the existing class to better suit your needs.

To put it simply, inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes.

Lets imagine your colleague has developed the following code for a company.  It has one class Staff and it can only calculate the total salary of the generat employees in your company.

In another words, you need specialization.  You need to extend it and calculate also the total salary of engineers in your company.

We will look at how we can extend the code below in the next post.

using System;

namespace Company
    public class Staff
        protected double rate;

        public Staff()
            rate = 1;

        public void salary()
            double totalSalary = 30*8*rate;


    class Program
        static void Main()
            Staff staff = new Staff();


PHP & OOP: The OOP core ideas

This entry is part 2 of 11 in the series PHP & OOP

What made a language object-oriented?

There are a few core ideas related to Object Oriented programming language.

  • Dynamic Dispatch – when a method is invoked on an object,
  • Encapsulation – in which case the state is kept separate
  • Inheritance – inheritance lets the structure and methods in one class pass down the hierarchy

Additional concepts are classes and objects and we will start off our series with classes and objects.