Inheritance and Polymorphism. Upcasting and Downcasting

 *Inheritance. Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).extends is the keyword used to inherit the properties of a class.

Example:

package Main;

public class Car {

private Integer speed;
private String color;
private Integer seatCount;

public Integer getSpeed() {
return speed;
}

public void setSpeed(Integer speed) {
this.speed = speed;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public Integer getSeatCount() {
return seatCount;
}

public void setSeatCount(Integer seatCount) {
this.seatCount = seatCount;
}
}
package Main;

public class BMW extends Car{
public int a;

public int foo(){
return 1;
}
}
package Main;

public class Main {

public static void main(String[] args) {
BMW b = new BMW();
int x = b.a;
b.foo();
b.getColor();
}
}

Upcasting and Downcasting

  1. Upcasting: Upcasting is the typecasting of a child object to a parent object. Upcasting can be done implicitly. Upcasting gives us the flexibility to access the parent class members but it is not possible to access all the child class members using this feature. Instead of all the members, we can access some specified members of the child class. For instance, we can access the overridden methods.
  2. Downcasting: Similarly, downcasting means the typecasting of a parent object to a child object. Downcasting cannot be implicitly.
package Main;

public class Parent {

String name;

void method(){
System.out.println("Method from Parent");
}

}
package Main;

public class Child extends Parent{

int id;

void method(){
System.out.println("Method from Child");
}

}
The output are:
Parvin
Method from Child
Parvin
1
Method from Child

package Main;

public class Main {

public static void main(String[] args) {

Parent p = new Parent();
Child c = (Child) p;

System.out.println(p.name);
p.method();

System.out.println(c.id);
System.out.println(c.name);
c.method();
}
}
The output is:
ClassCastException. 
The reason is Child object can not see the whole Child class it only sees Parent class.

*Upcasting will be done internally and due to upcasting the object is allowed to 
access only parent class members and child class specified members
(overridden methods, etc.) but not all members.

*The very curious question is why Downcasting cannot be done implicitly?
Answer:
package Main;

public class Car {

public Integer speed;
public String color;
public Integer seatCount;
}
package Main;

public class Mercedes extends Car {
String name = "Merce";
}
package Main;

public class BMW extends Car{
public int a = 5;

public int foo(){
return 1;
}
}
package Main;

public class Main {

public static void main(String[] args) {
Mercedes m = new Mercedes();
Car c = m;
//BMW b = c; - > RuntimeError
BMW b = (BMW) c;
}
}
Here we can easily notice runtime error, because BMW doesn't extend Mercedes class
so that in this code BMW b = c; Itellij doesn't allow to cast it, because according 
to Polymorphism c is an Mercedes object. However when we Downcast it explicitly
Intellij charges us for this responsibility. So that we have to Downcast.

Here below we have another example:

package Main;

public class Main {

public static void main(String[] args) {
Car c1 = new BMW();
BMW b1 = (BMW) c1;
System.out.println(b1.a);
System.out.println(b1.color);

Car c = new Car();
BMW b = (BMW) c;
System.out.println(b1.a);
System.out.println(b.color);
}
}
Output:
5
red
ClassCastException.
Let's analyse the reason of ClassCastException.
Let's think in this way: Car clas has an area of 1 km^2 and BMw has of course larger
area because it exdends Car class. Of course we can put small area into big area,
but it loses its functionality.There are still empty places and that causes the problem.

Another example:
package Main;

public class Main {

public static void main(String[] args) {
BMW b = new BMW();
Car c = b;
Mercedes m = (Mercedes) c;
System.out.println(c.color);
System.out.println(m.name);
}
}
Output:
ClassCastException.
Firstly we created BMW object, then Upcasted it to c(Car object) now c is a BMW object
And finally we are trying to Downcast it to Mercedes object, however Mercedes doesn't
extend BMW so we get output: ClassCastException.

Another example:
package Main;

public class Main {

public static void main(String[] args) {
BMW b = new BMW();
Car c = b;
BMW bb = c;

}
}
Here we have compile time error but hypothetically we do not have to get error.
 Let's decipher the code: firstly we create BMW object and the Upcasting it to Car 
object and finally we want to Downcast it to BMW object. But Intellij doesn't 
understand whether c is a BMW object or Mercedes object and it leaves us(this
responsibility) to do it explicitly so we are obliged to write:
BMW bb = (BMW) c;

*Polymorphism

A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.

Shortly Polymorphism is - when one object act like other different objects.

It is pretty simple. For example BMW extends Car. Here BMW inherit Car(Inheritance)

and act like a Car(Polymorphism).


Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class