Uploaded by Sidharth

Inheritance and Polymorphism in Java

advertisement
Inheritance
Inheritance is where one class acquires the properties (methods and fields) of another.
The extends keyword is the word that dictates which class the sub class extends from.
In this example, the Employee class extends the People class, so it inherits all the attributes
such as name, phoneNum, and halfLizard (whether they are a lizard or not). But to create
the object of an employee, you will have to enter values for all of these, as well as the
employee number. You can also create an object that is just for people, without having to
input the Employee Number.
In the constructor for the child class, employees, there is a ‘super’ instead of listing every
attribute of the parent class of which it inherits from.
Example 1
People class
Employee Class
Polymorphism
While inheritance lets us inherit attributes and methods from another class, polymorphism
uses those methods to perform different tasks. Polymorphism is the ability of an object to
take many forms, it is usually used in OOP when a parent class refers to a child class object.
This allows us to perform a single action in many different ways. There are two main types
of polymorphism: Compile time polymorphism and run time polymorphism.
1.Compile time polymorphism
Example 2
When there are multiple functions with the same name but different parameters, these
functions are said to be overloaded. This type of polymorphism is achieved by function
overloading or operator overloading. In this example, when I make a new object of class
‘Vehicles’, I don’t have to input all the parameters for it. A number of different
combinations of parameters come up so the user doesn’t have to input every single piece of
data about the object if some parameters are unnecessary or don’t exist for some objects of
the class (a vehicle bicycle doesn’t need maxCapacity). Depending on the parameters
entered, the code can do different things with the amount of info it has (I could make a
method to print the maxPassengers with the brand name, and another method of the same
name to print just the maxPassengers; if the brand name isn’t provided the second method
will be run). In the constructors, make sure you put ‘this.attribute = attribute’ for every
variable, but you make it equal to ‘null’ or ‘-1’ if you don’t want to have to enter it when
creating an object of that class.
2. Run time polymorphism
The same method can have different variations for different classes if needed. For example,
the method (movement) is different for the car class and for the boat class as they move
differently. The individual objects can go through different methods depending on their
class. In this example, the added bonus is that it prints the specific passenger capacity for
the boat and the top speed for the car. This is method overriding as opposed to method
overloading.
Arrays
Arrays can store multiple values in a single variable, instead of having to declare separate
variables for each value.
Example 3
In this example, we have an array called animals with five elements inside it. Remember to
put the brackets before the array name when declaring the array
Download