OOP Chapter III-1
OOP Chapter III-1
School of Informatics
Department of Computer Science
3.1.Inheritance
3.2.Casting
3.3. Polymorphism
3.4. Method Overriding and Overloading
3.5.Super
3.6.The Object Class
3.7.Abstract Classes
3.8.Interfaces
3.9.Using Interfaces
3.1.Inheritance
3
As you can see, the subclass B includes all of the members of its superclass, A. This is why
subOb can access i and j and call showij( ).
Also, inside sum( ), i and j can be referred to directly, as if they were part of B.
Even though A is a superclass for B, it is also a completely independent, stand-alone class.
Being a superclass for a subclass does not mean that the superclass cannot be used by itself.
Further, a subclass can be a superclass for another subclass.
The general form of a class declaration that inherits a superclass is shown here:
class subclass-name extends superclass-name {
// body of class
}
8 Type Casting in Java
In Java, type casting is a method or process that converts a one data type into another data
type in both ways manually and automatically.
The automatic conversion is done by the compiler and manual conversion performed by the
programmer.
Type casting and its types in the diagram.
9
Narrowing Type Casting: converting a higher data type into a lower one. It is also
known as explicit conversion or casting up.
It is done manually by the programmer. If we do not perform casting then the compiler
reports a compile-time error.
10
Widening Type Casting: converting a lower data type into a higher one. It is also known
as implicit conversion or casting down. It is done automatically. It is safe because there is
no chance to lose data. It takes place when:
Both data types must be compatible with each other.
The target type must be larger than the source type.
For example, the conversion between numeric data type to char or Boolean is not done
automatically.
Also, the char and Boolean data types are not compatible with each other.
Example:
12 public class WideningTypeCasting {
public static void main(String[] args) {
int x = 8;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
} output
Before conversion, the value is: 8
After conversion, the long value is: 8
After conversion, the float value is: 8.0
13 Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in different
ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
Here, we will focus on runtime polymorphism in java.
Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an
overridden method is resolved at runtime rather than compile-time.
14
Method Overriding and Overloading
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has
been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
15
In the above example, Animal and Dog both classes have a common property color. If
we print color property, it will print the color of current class by default.
To access the parent property, we need to use super keyword.
Abstract class
23
A class which is declared with the abstract keyword is known as an abstract class in Java. It
can have abstract and non-abstract methods (method with the body).
The major use of abstract classes and methods is to achieve abstraction in Java.
Abstraction is an important concept of object-oriented programming that allows us to hide
unnecessary details and only showing functionality to the user.
This allows us to manage complexity by omitting or hiding details with a simpler, higher-
level idea.
A practical example of abstraction can be motorbike brakes. We know what brake does.
When we apply the brake, the motorbike will stop. However, the working of the brake is
kept hidden from us.
The major advantage of hiding the working of the brake is that now the manufacturer can
implement brake differently for different motorbikes, however, what brake does will be the
same. Another example, sending SMS where you type the text and send the message. You
don't know the internal processing about the message delivery.
24