0% found this document useful (0 votes)
4 views51 pages

CH 3

Uploaded by

jsjshdhdhdhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views51 pages

CH 3

Uploaded by

jsjshdhdhdhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Ch-3

• OOP Concepts, Classes, and objects, Data abstraction, encapsulation, inheritance,


Polymorphism.
• Procedural and object-oriented programming paradigm;
• Object-Oriented Programming Using Java,
• Inheritance: Inheritance types, super and subclasses, member access rules, super
keyword,
• preventing inheritance: final classes and methods,
• the object class and its methods;
• Polymorphism: dynamic binding, method overriding, abstract classes and
methods;
• Interface: Interfaces vs Abstract classes, defining an interface, implement
interfaces, accessing implementations through interface references, extending
interface.
Inheritance Types
• Simple/single inheritance
class A {}
class B extends A{}
Multilevel inheritance
• Class A {}
• Class B extends A {}
• Class C extends B{}
.
.
.
Up to n number of levels
Hierarchical Inheritance

• 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.

returns the string representation of this object. Object obj=getObject()


Typecasting is the assessment of the value of one primitive data type to another
type. In java, there are two types of casting namely upcasting and downcasting as
follows:
1.Upcasting casting a subtype to a super type in an 1.downcasting refers to the procedure when subclass
upward direction to the inheritance tree. It is an automatic type refers to the object of the parent class is known as
procedure for which there are no efforts poured in to do so downcasting. If it is performed directly compiler gives an
where a sub-class object is referred by a superclass error as classcastexception is thrown at runtime. It is
reference variable. One can relate it with dynamic only achievable with the use of instanceof operator The
polymorphism. object which is already upcast, that object only can be
1. Implicit casting means class typecasting done by performed downcast.
the compiler without cast syntax.
2. Explicit casting means class typecasting done by
the programmer with cast syntax.
In order to perform class type casting we have to follow these two rules as follows:
1.Classes must be “IS-A-Relationship “
2.An object must have the property of a class in which it is going to cast
An IS-A relationship is inheritance. The classes which inherit are known as sub classes or child classes. On the other hand,
HAS-A relationship is composition.

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.

class Apple extends Fruit {

}
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 {

Table table = new Table();

}
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 SBI extends Bank{


float getRateOfInterest(){return 8.4f;}
}

class ICICI extends Bank{


float getRateOfInterest(){return 7.3f;}
}

class AXIS extends Bank{


float getRateOfInterest(){return 9.7f;}
}

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:

Parent p = new Child();


1.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.

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.

c.name = p.name; i.e., c.name = "GeeksforGeeks"


// Java program to demonstrate // Demo class to see the difference
// Upcasting Vs Downcasting // between upcasting and downcasting
public class GFG {
// Parent class
class Parent { // Driver code
String name; public static void main(String[] args)
{
// A method which prints the // Upcasting
// signature of the parent class Parent p = new Child();
void method() p.name = "GeeksforGeeks";
{
System.out.println("Method from Parent"); //Printing the parentclass name
} System.out.println(p.name);
} //parent class method is overriden method
hence this will be executed
// Child class p.method();
class Child extends Parent {
int id; // Trying to Downcasting Implicitly
// Overriding the parent method // Child c = new Parent(); - > compile time error
// to print the signature of the // Downcasting Explicitly
// child class Child c = (Child)p;
@Override void method() c.id = 1;
{ System.out.println(c.name);
System.out.println("Method from Child"); }} System.out.println(c.id);
c.method(); }}
Polymorphism: dynamic binding

Connecting a method call to the method body is known as binding.


There are two types of binding
1.Static Binding (also known as Early Binding).
2.Dynamic Binding (also known as Late Binding).
static binding
When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
Example of static binding
1.class Dog{
2. private void eat(){System.out.println("dog is eating...");}
3.
4. public static void main(String args[]){
5. Dog d1=new Dog();
6. d1.eat();
7. }
8.}
Dynamic binding
When type of the object is determined at run-time, it is known as dynamic binding.
Example of dynamic binding
1.class Animal{
2. void eat(){System.out.println("animal is eating...");}
3.}
4.
5.class Dog extends Animal{
6. void eat(){System.out.println("dog is eating...");}
7.
8. public static void main(String args[]){
9. Animal a=new Dog();
10. a.eat();
11. }
12.}

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.

You might also like