INTERFACES - JAVA: Learn JAVA Quickly (2017)

JAVA: Learn JAVA Quickly (2017)

CHAPTER 9. INTERFACES

Interfaces are not classes. They only contain methods and attributes whose class that implements that interfaces need to have. You can recognize first difference from abstract classes, interfaces are implementing by keyword implements, not extending. All methods are implicit public and all attributes are implicit public static final. Let`s now make out first interface by right click on current package New ->Interface and name it House. We will add following lines:

String bed = "My bed";

void sleep();

So, as you can see our method sleep() don`t have implementation. That is because we don`t need to know how method must be implemented at this moment. To understand better, imagine that you have interface Vehiclewith methods like start() and stop(), and classes which implement that interface like Car, Bicycle and Airplane. At the moment of creation of Vehicle interface you only know that you would have some different type in future, that we mentioned, but you actually don`t know how they work because all work different. Think in that way about interfaces.

But, from Java 8, interfaces are changed. Now you can have default method:

defaultvoid watchTV() {

System.out.println("I am wathcing TV");

}

As you can guess, this means that now interfaces are more similar to abstract classes. One thing that is very important is that you can implement multiple interfaces, rather that only one class.

Both, abstract classes and interfaces use inheritance. Inheritance is type of code reusing. This is very useful because in development you will be in situations where there are some classes with some methods and attributes that you may need. When class is extended and interface implemented, subclass need to override all their methods. Why we told that only one class can be extended?

Class A is super class and class D extends both class B and class C which have method with same signaturedoStuf() but they have different implementation. Which one should compiler choose? Situations like this are reason why creators of Java left out multiple inheritance. This is called a Diamond problem.

Note: Inheritance is is – a relationship, Student is a Person; Car is a Vehicle.

ASSOCIATION VS AGGREGATION VS COMPOSITION

When two objects are related to each other they are in association. Both composition and aggregation are types of association.

COMPOSITION

Inheritance is not only type of code reusing. Composition is another one, whose is has – a relationship between classes. With inheritance, composition is main technique for code reusing. Composition is relationship where part can`t exists without a whole.

publicclass Thought {

}

publicclass Brain {

Thought thought = newThought();

}

Brain has a thought. Thought can`t exists without brain. Composition has a stronger relationship then aggregation. Code reusing is here implemented by using object reference, thought, to use class which you need. Composition is special case of aggregation, it is more restricted.

AGGREGATION

Aggregation is weaker relationship then composition. In aggregation one part can exists without main object. For example: Bed and Pillow. Bed have many pillows and without pillow bed is functional. Also, without bed, pillow can exists for another purpose.

publicclass Pillow {

}

publicclass Bed {

public List<Pillow>pillows = new ArrayList<Pillow>();

}

OVERRIDING VS OVERLOADING

OVERRIDING

Overriding is term that we already saw in our examples. If in subclass is method that have same name and same input parameters like method in super class, then we have overriding. Usage of this is to change implementation in our subclasses. Do you remember example with vehicles? When method start() is derived to subclass Car or Airplane, implementation is different. Car and Airplane have totally different engines.

voidstart() {

System.out.println("Starting car engine");

}

voidstart() {

System.out.println("Starting airplane engine");

}

Note: Maybe it would be little confusing when it says implementation but inside method body is only println. This are only simple examples, in real development there will be some logic and things like that.

OVERLOADING

In overriding section we said that derived methods have same input parameters. If you didn`t figured out input parameters are inside curly braces of method declaration like: (String n, inta). That is not case with overloading. Methods are overloaded when they have same signature but different input parameters, of course in same class. We will explain this on constructor overloading. For example, make class StudentClass and add attributes name, age and diploma:

private String name;

privateintage;

private String diploma;

StudentClass(String n, inta) {

this.name = n;

this.age = a;

}

StudentClass(String n, inta, String d) {

this.name = n;

this.age = a;

this.diploma = d;

}

Constructors are overloaded. But what is usage of this? Imagine that you have class Faculty that manages with students. Not all students are same. Until finishing studies student don`t have diploma, so if you need some operations with students like this constructor without diploma parameter will be used. On another hand, if you want to store students that are finished studies to some document, constructor with diploma parameter will be useful. Beside constructors, methods also could be overloaded:

void doSomething(String a){

System.out.println("Print a: " + a);

}

void doSomething(String a, String b){

System.out.println("Print both a and b: " + a + b);

}

Both overriding and overloading are types of polymorphism. Polymorphism means that one method can do different things based on used object. Polymorphism in Java have two types:

· Compile time polymorphism

As we already sad, compile time is time while you type your code and compiler automatically recognized if you have some errors. So, compile time is decided in that time. Perfect example of this type of polymorphism is overloading. Compile time polymorphism is also called static binding.

· Runtime polymorphism

Overriding is type of runtime polymorphism. Unlike static binding, this type of polymorphism is called dynamic binding. From name you can guess that an overridden method is recognized at runtime.Upcasting is when reference variable of parent class refers to the object of child class we. JVM decides about overriding methods, not compiler.

Note: JVM (Java Virtual Machine) is an abstract machine that enables you to run a Java program on computer.

SUBHEADING 1

Chapter Text

SUB SUBHEADING

Chapter Text