About | Show
Tease your mind and test your learning, with these quick introductions to challenging concepts in Java programming. Compare inheritance and composition, the two fundamental ways to relate Java classes, then practice debugging ClassCastExceptions in Java inheritance.Koto Feja / Getty Images Table of Contents Show More Inheritance and composition are two programming techniques developers use to establish relationships between classes and objects. Whereas inheritance derives one class from another, composition defines a class as the sum of its parts. Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code. Classes and objects created through composition are loosely coupled, meaning that you can more easily change the component parts without breaking your code. Because loosely coupled code offers more flexibility, many developers have learned that composition is a better technique than inheritance, but the truth is more complex. Choosing a programming tool is similar to choosing the correct kitchen tool: You wouldn't use a butter knife to cut vegetables, and in the same way you shouldn't choose composition for every programming scenario. In this Java Challenger you'll learn the difference between inheritance and composition and
how to decide which is correct for your program. Next, I'll introduce you to several important but challenging aspects of Java inheritance: method overriding, the When to use inheritance in JavaIn object-oriented programming, we can use inheritance when we know there is an "is a" relationship between a child and its parent class. Some examples would be:
In each case, the child or subclass is a specialized version of the parent or superclass. Inheriting from the superclass is an example of code reuse. To better understand this
relationship, take a moment to study the
When you are considering using inheritance, ask yourself whether the subclass really is a more specialized version of the superclass. In this case, a car is a type of vehicle, so the inheritance relationship makes sense. When to use composition in JavaIn object-oriented programming, we can use composition in cases where one object "has" (or is part of) another object. Some examples would be:
To better understand this type of relationship, consider the composition of a
In this case, we know that a house has a living room and a bedroom, so we can use the Inheritance vs composition: Two examplesConsider the following code. Is this a good example of inheritance?
In this case, the answer is no. The child class inherits many methods that it will never use, resulting in tightly coupled code that is both confusing and difficult to maintain. If you look closely, it is also clear that this code does not pass the "is a" test. Now let's try the same example using composition:
Using composition for this scenario allows the Method overriding with Java inheritanceInheritance allows us to reuse the methods and other attributes of one class in a new class, which is very convenient. But for inheritance to really work, we also need to be
able to change some of the inherited behavior within our new subclass. For instance, we might want to specialize the sound a
This is an example of Java inheritance with method overriding. First, we extend the Does Java have multiple inheritance?Unlike some languages, such as C++, Java does not allow multiple inheritance with classes. You can use multiple inheritance with interfaces, however. The difference between a class and an interface, in this case, is that interfaces don't keep state. If you attempt multiple inheritance like I have below, the code won't compile:
A solution using classes would be to inherit one-by-one:
Another solution is to replace the classes with interfaces:
Using ‘super' to access parent classes methodsWhen two classes are related through inheritance, the child class must be able to access every accessible field, method, or constructor of its parent class. In Java, we use the reserved word
In this example, Using constructors with inheritanceWhen one class inherits from another, the superclass's constructor always will be loaded first, before loading its subclass. In most cases, the reserved word
If the parent class has a constructor with at least one parameter, then we must declare the constructor in the subclass and use
Type casting and the ClassCastExceptionCasting is a way of explicitly communicating to the compiler that you really do intend to convert a given type. It's like
saying, "Hey, JVM, I know what I'm doing so please cast this class with this type." If a class you've cast isn't compatible with the class type you declared, you will get a In inheritance, we can assign the child class to the parent class without casting but we can't assign a parent class to the child class without using casting. Consider the following example:
When we try to cast an The problem in this case is that we've instantiated
Then tried to cast it like this:
Because we don't have a In order to avoid the exception, we should
instantiate the
then assign it to
In this case, because we've extended the Casting with supertypesIt's possible to declare a
You can also use casting without assigning the object to a class type. This approach is handy when you don't want to declare another variable:
Take the Java inheritance challenge!You've learned some important concepts of inheritance, so now it's time to try out an inheritance challenge. To start, study the following code: Which classes Cannot be inherited?An abstract class cannot be inherited by structures.
Which is not inheritance in Java?Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. If we define Parent class constructor inside Child class it will give compile time error for return type and consider it a method.
Can every class be inherited in Java?All classes in Java directly or indirectly inherit the Object class. Inheritance is an object-oriented concept in which one class uses the properties and behavior of another class. There are various types of inheritance supported in Java such as: Single Level inheritance.
|