Example:
interface Printable { void print(); String DEFAULT_COLOR = "BLACK"; String ERROR_COLOR = "RED"; }A class is a construct that is used to create instances of itself referred as objects. A class has members which enable it's instances to have state and behavior. We can say that an animal is an instance of the class of objects known as animals.
Example:
public class Animal { int age = 0; int weight = 0; void speak(){ System.out.println("I am an animal and can speak"); } }
The fields age, weight represent the object's state, and the method speak define its interaction with the outside world. Abstract classes can have or not abstract methods and cannot be instantiated, but they can be sub-classed. An abstract methods is a methods that has just the declaration (as we did for interfaces, without braces, and followed by a semicolon).
Example:
public abstract class Animal { int age = 0; int weight = 0; abstract void speak(); }
When an abstract class is subclassed, the subclass many times provides implementations for all of the abstract methods in its parent class, if there is at least one abstract method without implementation then the subclass must also be declared abstract.
Differences between interfaces and abstract classes:
- interfaces can contain only fields that are static and final;
- abstract classes can contain fields that are not static and final; - interfaces can contain just methods signatures;
- abstract classes can contain implemented methods;
- interfaces can help on multiple inheritance (example: implements Comparable, Printable);
- abstract classes are usually sub-classed to share pieces of implementation.
Polymorphism is the ability of an object to take on many forms. There are three types of polymorphism: ad-hoc (overloading and overriding), parametric (generics) and dynamic method binding(late binding).
The signature of a method is the combination of the method's name along with the number and types of the parameters (and their order).
Overloaded methods are methods with the same name signature but with a different number of parameters or different types in the parameter list.
Overridden methods are methods that are redefined within a subclass.
Java parametric polymorphism is called generics and implemented through type erasure. This design decision was made to ensure backwards compatibility and ensure that Java generics are interoperable with non-generic code.
Dynamic (or late biding) method is the ability of a program to resolve references to subclass methods at runtime.
Suppose you have two subclasses Horse and Lion:
public abstract class Animal { public abstract void eat; }
public class Horse extends Animal { public void eat() { System.out.println("Eats grass"); } }
public class Lion extends Animal { public void eat() { System.out.println("Eats meat"); } }
And now you have a list of animals you can just do this and it will print to correct eat:
Listanimals = new List (); animals.add(new Lion()); animals.add(new Horse()); for(Animal a: animals) { a.eat(); }
Inheritance is used when there is a is-a relationship between a class and another (the lion is an animal). Composition is used when there is a has-a relationship between a class and another (a house have doors).
As a general rule prefer composition over inheritance because:
- a subclass depends on the inherited implementation details for its function and when the super-class's implementation it's changed the subclass functionality might brake;
- inheritance breaks encapsulation by exposing subclasses to implementation details in the super-class;
- for code reuse.
No comments:
Post a Comment