DEV Community

Cover image for Object Oriented Programming and Java
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Object Oriented Programming and Java

Introduction

  • This series is going to be a collection of my notes while I read Object Oriented Programming and Java Second Edition. It is an expensive book, but this is the internet, so I am sure you can find a cheaper version of the book somewhere. I have also made a YouTube version of this post so please check that out as well. Also here is a link to the GitHub with the code

Inheritance

  • In object-oriented programming(OOP) inheritance is very important for code reuse. Inheritance enables super classes to have their properties be propagated downward to the subclasses. Now that we have talked a little about inheritance I will post all of the code and then we will both walk through it line by line. Our code will consist of a Person class, a Employee class, a Manager class and a Secretary class.
public class Person {
    private String name;

    Person(String name){
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

}
Enter fullscreen mode Exit fullscreen mode

Person class

  • This Person class is going to be our main super class, it sits at the top of our class hierarchy and every other class will inherit from it. We can see that it is a very basic class, it has a instance variable of type String called name and and a getter method called getName(). Finally we see the method that has the same name as the class, this is a constructor. But what really is a constructor?

What is a constructor?

  • Well constructors are methods that have the same name as their containing class. We can even have more than one constructor as long as their method signatures are different. A method signature being a combination of the method name, its parameter type and the number of parameters. If we define two identical constructors then we will get a compile time error.

  • Not all classes need a constructor, if we do not define a constructor the Java compiler will define one for us. This constructor will be a default constructor will no parameters. When dealing with inheritance, having a default constructor should be used with caution because it will call the super classes' default constructor method. If no constructor exists in the super class then a compile error will be created.(more on this later in the post). Now lets move on to the Employee class.

public class Employee extends Person{

    private float basicSalary;
    private String employeeNumber;

    Employee(String name,String employeeNumber,float basicSalary){
        super(name);
        this.employeeNumber = employeeNumber;
        this.basicSalary = basicSalary;
    }

    public String getEmployeeNumber() {return employeeNumber;}
    public float getBasicSalary() {return basicSalary;}

    //WE ARE RUNNING STUFF FROM HERE
    public static void main(String[] args) {
        Manager m = new Manager("Bob1","01234M",9000.0f,2000.0f);
        Secretery s = new Secretery("Bob2","98765s",2500.0f);

        System.out.print("The Manager" + m.getName() + "Employee number ->" + m.getEmployeeNumber());
        System.out.println("has a salary of" + m.getBasicSalary());
        System.out.println("Secretery has a salary of" + s.getBasicSalary()+ "Employee number ->" + s.getEmployeeNumber());

        System.out.print("The Manager " + m.getName());
        System.out.println("has a allowance of" + m.getAllowance());


    }
}

Enter fullscreen mode Exit fullscreen mode

Employee class

  • First notice the main method, indicating that this is going to be the starting point of our application. The next thing that you should notice is the extends keyword, lets talk about that.

Extends

  • This keyword tells us that the Employee class is a subclass of the Person class, but what is a subclass? Good question!

What is a Subclass?

  • According to the documentation, " a subclass inherits all of the public and protected members of its parent, no matter the package. If the subclass is in the same package as the parent, it also inherits the package-private member of the parent.". In typical documentation style, it raises more questions than it answers. What is a package?, what are private, protected and package-private? So lets answer those questions and then we can understand what a subclass is.

What is a package?

  • A package is a way to organize related classes, in this regard packages are similar to files. If packages are similar to files then why have both? Well, Packages actually differ from files in 3 main ways.

1) Resolving name conflicts : packages allow us to do things like this com.zzz.Circle and com.yyy.Circle, which although they share the same name Circle they are actually two different classes and can be treated in such. This type of flexability is called Name space management.

2) Distribution : packages allow for distribution of a collection of reusable classes, usually in a format known as Java Archive (JAR) file.

3) Access control : there are access control modifiers, protected and package protected, (more on this later) that are specific to packages. They define what is accessible from one package to another.

What is a access control modifier?

  • These modifiers specify which classes and methods can use modified classes, methods and variables. There are 4 control modifiers but when nothing is defined then the access modifier is defaulted to package private.

1) public : this allows any class or method to access what the public keyword has modified.

2) private : limits access only to the class.

3) package private (default) : this is the default when no modifiers are specified. All classes within the same package have access to what is modified. However, subclasses from a different package do not have access to a super class' package private methods and variables.

4) protected : has the same restrictions as package private but it allows subclasses access.

Instance variables

  • Instance variables are just variables that get new values upon each new instantiation of a object. In Employee class the instance variables are private float basicSalary and private String employeeNumber. the private String should be familiar to us. All it is saying is that there is a variable of type string that is only accessible to methods inside this class. But what is type float? To put it lightly float is a primitive data type that can be used to hold number with decimals but it is not as accurate as a double type. You will also notice number like this 2000.0f the f is to tell the compiler that this is of type float. If there is no f then the compiler will assume that the type is a double. If you want to get really into the technical weeds of what a float is, then I would suggest that you google single-precision 32-bit IEEE 754 floating point. That will lead you down a floating point arithmetic rabbit hole.

The constructor

  • Now lets move on to the constructor of Employee class. You will notice the method super(name). In this instance we are using the super method to invoke the super class' constructor and giving it the parameter of name.

  • When we have a constructor that is a subclass and we do not define a super(...args) method, the Java compiler will create its own super() method in the constructor. If the super class does not have a no argument constructor then we will get a compile time error. You can notice this error yourself if you delete the super(name) from the employee constructor.

Secretary class

public class Secretery extends Employee{
    Secretery(String name, String employeeNumber, float basicSalary){
        super(name,employeeNumber,basicSalary);
    }
}

Enter fullscreen mode Exit fullscreen mode
  • This is a very simple class that extends the Employee class, all stuff we have seen before. However, when the call to constructor happens, we call the super method which invokes the Employee constructor, which will then invokes the Person constructor. The chain of constructors being fired off is called constructor chaining.

Manager class

public class Manager extends Employee{
    private float allowance;

    Manager(String name, String employeeNumber, float basicSalary, float allowanceAmt){
        super(name,employeeNumber,basicSalary);

        this.allowance = allowanceAmt;
    }

    public float getAllowance() {
        return allowance;
    }
}

Enter fullscreen mode Exit fullscreen mode
  • I have very little to say about this class because it does everything that we have already talked about. It is a basic Java class.

Employee main method

  • The main method acts as the entry point for our application, when we run this application it will start with the main method. It should also be noted that you should never change the name of the main method. If you change the name of the main method, it will stop being a main method.

  • Lastly all we have to do is to run the application and we should see everything printed out to the console.

System.out.print()

  • This is just how we get information from out application to the console. The System class gives us access to input and output streams that allow us to get data in and out of our application. out is the output stream that System provides us and its output destination is predefined by the environment. If you run this code in eclipse then the predefined destination is the eclipse console.print() is what allows us to print to the console. If you wish to learn more about Java streams here is a Link.

References

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)