Skip to the content.

What Is Inheritance?

Example : Many kind of obejcts in real life have common properties. yet they also have things that make them different.

The syntax for creating a subclass :

At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

// new fields and methods defining 
// a mountain bike would go here

}

The Java Platform Class Hierarchy

All Classes in the Java Platform are Descendants of Object

All Classes in the Java Platform are Descendants of Object

What You Can Do in a Subclass?

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

What Is an Interface?

Example : the buttons on the front of your television set,are the interface between you and the electrical wiring on the other side of its plastic casing.

objects define their interaction with the outside world through the methods that they expose,Methods form the object’s interface with the outside world

an interface is a group of related methods with empty bodies.

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Interfaces in Java

Define an enter face:

public interface OperateCar {

}

To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface.

public class OperateBMW760i implements OperateCar {

}