A class can inherit from another class. For example, suppose you have a class Animal:
public class Animal
{
public void Walk() {}
}
And you define a second class Dog:
public class Dog extends Animal
{
}
Now you can call Walk() on any Dog you instantiate. Dog inherits all methods and variables of Animal. Animal is the superclass, and Dog is the subclass.
Overriding
If you write a Walk() function for Dog, that function will override Animal's Walk() function. To call Animal's Walk() function from the Dog class, say super.Walk().
What Gets Inherited
At least in java:
public members are inherited
private members are not inherited.
See "IS-A and HAS-A" to see if you have a good inheritance relationship.
It's not a good idea to have the variables of a class exposed. You don't want users to be able to do things like this:
Dog.height = -1;
Encapsulation refers to methods that provide access to variables instead, like so:
private int height;
public void setHeight(int a_height)
{
if (a_height > 0) {height = a_height;}
}
public int getHeight()
{
return height;
}
Notice how we've marked the variable as "private" so no random yahoo can access it.
Following Encapsulation, Getters are the functions that return a variable:
public int getHeight()
{
return height;
}
Following Encapsulation, Setters are the functions that change a variable's value:
public void setHeight(int a_height)
{
if (a_height > 0) {height = a_height;}
}
In the Inheritance example, we can say that the two classes have an "IS-A" relationship...a Dog IS-A Animal. This is a good way to check if your inheritance tree makes sense. For example, you might think that "Claw" should inherit from Animal. But Claw IS-A Animal doesn't make sense. So this inheritance doesn't make sense. What you want instead is a HAS-A relationship...Animal HAS-A Claw. So Animal has an instance variable Claw.
A typical variable declaration and assignment:
Dog a = new Dog();
With polymorphism, you can do this instead (assuming Dog is a subclass of Animal):
Animal a = new Dog();
Why is this useful? Consider the following code, which assumes that Dog, Cat, Wolf, Hippo and Lion all extend (aka inherit from, aka subclass) Animal:
Animal[] animals = new Animal[5];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Wolf();
animals[3] = new Hippo();
animals[4] = new Lion();
for (int i = 0; i < animals.length;i++)
{
// if this is a dog, it calls the Dog's eat method,
// if it's a cat, it calls the Cat's eat method etc
animals[i].eat();
// same thing here.
animals[i].roam();
}
You can also use polymorphism while passing arguments, like so:
// Here you can pass a Dog, a Cat, any other class you
// can cook up that inherits from Animal
public void giveShot(Animal a)
{
a.makeNoise();
}
But keep in mind that you can only call methods that are on the superclass. So suppose Dog has a method rollOver() but Animal does not. You cannot write this:
Animal a = new Dog();
a.rollOver();
Overloading lets you make multiple versions of a method with different argument lists. Here's an example of an overloaded method "Add":
public int Add(int x, int y)
{
return x + y;
}
public String Add(String a, String b)
{
return a+" "+b;
}
The JVM will automatically use the right function depending on the arguments. To overload a method, you must change it's arguments. Otherwise it's considered an override. For example, you can't have two methods with exactly the same arguments but different return types. That's not overloading, that's overriding.
We have two classes Dog and Cat that inherit from a class Animal. We want to be able to do this:
Dog d = new Dog();
Cat c = new Cat();
We don't want to be able to do this:
Animal a = new Animal();
So Animal is an abstract class, because we don't want it to be able to be instantiated. Here's how you make a class abstract:
abstract class Animal {
}
You can still use an abstract class in polymorphism though:
Animal a = new Dog(); // can do this
Animal[ ] animals = new Animal[5]; // can do this
Animal b = new Animal(); // can't do this
An abstract method is written like this:
public abstract void eat();
Abstract methods can't be in non-abstract classes, so if you mark a method as abstract, you have to mark the class abstract too.
Abstract methods are useful for polymorphism. What you're saying is "I can't write one method that would cover all of my subclasses, but each subclass does indeed have this method. Just one that they implement themselves because it's so unique to each one."
All concrete subclasses must inherit all the abstract methods of it's superclass. So if you have an abstract class Animal with an abstract method eat(), and then you make a concrete class Dog, Dog has to implement eat(). If you add an abstract class Canine so that your inheritance tree looks like Animal->Canine->Dog, Canine may or may not implement eat(). It has a choice, because it is an abstract class. If it does implement eat, Dog doesn't need to. If it doesn't, then Dog needs to.
An interface is like a 100% pure abstract class. It's defined like this:
public interface Pet {...}
It's implemented like this:
public class Dog extends Animal implements Pet {...}
You can't have multiple inheritance in some languages (like Java), so interfaces come in useful because you can use polymorphism with interfaces.
You can also have a class implement multiple interfaces, like so:
public class Dog extends Animal implements Pet, Friend, Companion {...}
Runs when an object is created. Looks like this:
// Java
public Dog()
{
}
Static methods are those that don't need an instance variable to run...you can call them straight from the class itself. Here's some static methods you've seen:
TweenLite.to(mc,1,{alpha:1});
Math.round(21.4);
To make a static method, just mark it static like so:
public static int round (int num) {
}
Static methods don't have an instance, so they can't use any instance variables or call any non-static methods. They can use static variables and call static methods though. Or call a non-static method on a specific object, like duck1.quack().
In an instance variable, each instance of a class gets it's own copy of the variable. A static variable is global to a class. There is only that one copy. This is useful for keeping track of global values, like how many Dog objects have been created so far. You declare it like this:
static int dogCount = 0;
Notice how we gave it an initial value.