Skip to main content

Posts

Showing posts from December, 2013

In Java, how does System.out.println() work?

In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable. What is “out” in System.out.println()? We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? There are two possibilities – it could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we kn

In Java, what’s the difference between method overloading and method overriding?

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly simple to understand. Let’s start the discussion by talking more about method overloading. Overloading in Java  can  occur when two or more methods in the same class share the same name or even if a child class shares a method with the same name as one of it’s parent classes. But, in order to  actually  have overloaded methods, the methods not only have to have the same name, but there are other conditions that must be satisfied – read below to see what those conditions are. Suppose we have a class called TestClass which has 2 methods, and both methods have the  same  name – let’s say that name is “someMethod” – this would be considered to be method overloading if  at least one of these 2 things is true: 1.) The number of parameters is different for the methods 2.) The parameter types are different. How to NOT overload methods: It’s important to underst

In Java, what’s the difference between an object and a class?

The terms ‘class’ and ‘object’ are absolutely related to one another, but each term holds its own distinct meaning. The term ‘class’ refers to the actual written piece of code in which the class is defined. A class is a static piece of code that consists of attributes which don’t change during the execution of a program – like the method definitions within a class. An object is an instance of a class The term ‘object’, however, refers to an actual  instance  of a class. Every object must belong to a class. Objects are created and eventually destroyed – so they only live in the program for a limited time. While objects are ‘living’ their properties may also be changed signficantly. An example will help clarify what we’ve said. Suppose we have a class called ‘Animal’. All Animals have bodies and brains. So, this general ‘template’ of an Animal does not change. An instance of the Animal class would be a  specific  animal – like a lion, a cat, or a zebra. These instances of t

What’s the difference between an interface and an abstract class in Java?

A class must be declared  abstract  when it has one or more abstract  methods . A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do. When to use abstract methods in Java? Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below: /* the Figure class must be declared as abstract because it contains an abstract method */ public abstract class Figure { /* because this is an abstract method the body will be blank */ public abstract float getArea(); } public class Circle extends Figure { private float radius; public float getArea() { return (3.14 * (radius * 2)); } } public class Rectangle extends Figure { private float length, width; public float getArea(Figure other) { return length * width; } } In the Figure class above, we have an abstract method called getAre