CH 3
CH 3
• Class A{}
• Class B extends A{}
• Class C extends A{}
• Class D extends A {}
•.
•.
•.
Hybrid inheritance
• Combination of two or more than two inheritance
object class and its methods
The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of
java.
In OOP, IS-A relationship is completely inheritance. This means, that the child class is a type of parent class. For example, an
apple is a fruit. So you will extend fruit to get apple.
}
On the other hand, composition means creating instances which have references to other objects. For example, a room has a
table. So you will create a class room and then in that class create an instance of type table.
class Room {
}
A HAS-A relationship is dynamic (run time) binding while inheritance is a static (compile time) binding. If you just want to
reuse the code and you know that the two are not of same kind use composition. For example, you cannot inherit an oven
from a kitchen. A kitchen HAS-A oven. When you feel there is a natural relationship like Apple is a Fruit use inheritance.
import java.io.*; class demo
{
class Parent
{ public static void main(String[] args)
void show() {
{ // Creating a Parent class object
System.out.println("Parent show method is called"); // but referencing it to a Child class
} Parent obj = new Child();
}
} // Calling the show() method to execute
class Child extends Parent obj.show();
{ }
void show() }
{
System.out.println(“Child show method is called");
}
}
class Bank{
float getRateOfInterest(){return 0;}
}
class TestPolymorphism{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
b=new AXIS();
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
}
}
import java.io.*;
class Vehicles {void show()
{
System.out.println("hello");
}
}
class Car extends Vehicles {
static void method(Vehicles v)
{
if (v instanceof Car) {
// Downcasting
Car c = (Car)v;
System.out.println("Downcasting performed");
}
}
public static void main(String[] args)
{
// Creating an object of Vehicle class
// and referring it to Car class
Vehicles v = new Car();
Car.method(v); v.show();
}
}
NOTE : Without perform upcast if we try to downcast , ClassCastException will
be thrown.
•It is a runtime exception or unchecked exception.
•It is class, present in java.lang package.
•It can be avoided by using a operator known as ‘instanceof’.
Typecasting is one of the most important concepts which basically deals with the conversion of one
data type to another datatype implicitly or explicitly. In this article, the concept of typecasting for
objects is discussed.
Just like the data types, the objects can also be typecasted. However, in objects, there are only two
types of objects, i.e. parent object and child object. Therefore, typecasting of objects basically means
that one type of object (i.e.) child or parent to another. There are two types of typecasting. They are:
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 implicit.
The following image illustrates the concept of upcasting and downcasting:
Example: Let there be a parent class. There can be many children of a parent. Let’s take one of
the children into consideration. The child inherits the properties of the parent. Therefore, there is an
“is-a” relationship between the child and parent. Therefore, the child can be implicitly upcasted to
the parent. However, a parent may or may not inherits the child’s properties. However, we can
forcefully cast a parent to a child which is known as downcasting. After we define this type of
casting explicitly, the compiler checks in the background if this type of casting is possible or not. If
it’s not possible, the compiler throws a classcastexception
Let’s understand the following code to understand the difference:
1.Syntax of Upcasting:
1.Syntax of Downcasting:
Child c = (Child)p;
1.Downcasting has to be done externally and due to downcasting a child object can acquire the
properties of the parent object.
In the above example object type cannot be determined by the compiler, because the instance of Dog
is also an instance of Animal. So compiler doesn't know its type, only its base type.