Chapter 1 - Unit 2
Chapter 1 - Unit 2
Array of Objects
We know that an array is a collection of the same data type that dynamically creates objects
and can have elements of primitive types. Java allows us to store objects in an array. In Java,
the class is also a user-defined data type. An array that conations class type elements are
known as an array of objects. It stores the reference variable of the object.
Note: In Java, we can create arrays by using new operator and we know that every object is
created using new operator. Hence we can say that array is also an object.
Before creating an array of objects, we must create an instance of the class by using the new
keyword.
We can use any of the following statements to create an array of objects.
Syntax:
//declare and instantiate an array of objects.
ClassName objname[]=new ClassName[array_length];
Or
ClassName[] objArray;
Or
ClassName objArray[];
Suppose, we have created a class named Employee. We want to keep records of 20 employees
of a company having three departments. In this case, we will not create 20 separate variables.
Instead of this, we will create an array of objects, as follows.
Employee department1[20];
Employee department2[20];
Employee department3[20];
The above statements create an array of objects with 20 elements.
Example:
class student
int sid1;
String sname1;
sid1 = sid2;
sname1 = sname2;
void display()
System.out.println();
class ArrayOfObjects
obj[0].display();
obj[1].display();
obj[2].display();
obj[3].display();
obj[4].display();
Output :
Inheritance: Basic concepts
Inheritance is a mechanism in which one object acquires all the properties and behaviors of a
parent object.
It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Why use inheritance:
o For Code Reusability.
Terms used in Inheritance:
o Class: A class is a group of objects which have common properties. It is a template or blueprint
from which objects are created.
o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is
also called a base class or a parent class.
o Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the
fields and methods of the existing class when you create a new class. You can use the same
fields and methods already defined in the previous class.
class one
{
void print1()
{
System.out.println("This is Base Class method");
}
}
class two extends one
{
void print2()
{
System.out.println("This is Derived Class method");
}
}
class demo
{
public static void main(String[] args)
{
one o = new one();
o.print1();
two t = new two();
t.print1();
t.print2();
}
}
Output :
2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as
well as the derived class also act as the base class to other class. In the below image, class A serves as a
base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a
class cannot directly access the grandparent’s members.
class one
{
void print1()
{
System.out.println("This is Base Class method");
}
}
class two extends one
{
void print2()
{
System.out.println("This is Intermediate Class method");
}
}
class three extends two
{
void print3()
{
System.out.println("This is Derived Class method");
}
}
class demo
{
public static void main(String[] args)
{
one o = new one();
o.print1();
two t = new two();
t.print1();
t.print2();
three th = new three();
th.print1();
th.print2();
th.print3();
}
}
Output :
3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for
more than one subclass. In the below image, class A serves as a base class for the derived class B, C and
D.
class one
void print1()
}
}
void print2()
void print3()
void print4()
class demo
o.print1();
t.print2();
th.print1();
th.print3();
f.print1();
f.print4();
Output :
Example 1:
class base
void show()
void show()
class Override
{
obj.show();
Output:
Example 2:
class A
int i, j;
A(int a, int b)
i = a; j = b;
// display i and j
void show()
class B extends A
int k;
B(int a, int b, int c)
super(a, b);
k = c;
void show()
class Override
k: 3
Note:
o Method overriding is one of the way by which java achieve Run Time Polymorphism.
o The version of a method that is executed will be determined by the object that is used to invoke
it.
o If an object of a parent class is used to invoke the method, then the version in the parent class
will be executed, but if an object of the subclass is used to invoke the method, then the version
in the child class will be executed.
o In other words, it is the type of the object being referred to (not the type of the reference
variable) that determines which version of an overridden method will be executed.
class base
{
int a = 100;
}
class derived extends base
{
int a = 200;
void display()
{
System.out.println("Super class member: " + super.a);
System.out.println("sub class member: " + a);
}
}
class Test
{
public static void main(String[] args)
{
derived d = new derived();
d.display();
}
}
Output
Super class member : 100
Subclass member : 200
Note: In the above example, both super class and subclass have a member ‘a’. We could access ‘a’ of
base class in subclass using super keyword.
Example of SecondPoint:
Use of super with methods: This is used when we want to call parent class method. So whenever a
parent and child class have same named methods then to resolve ambiguity we use super keyword.
This code snippet helps to understand the said usage of super keyword.
class base
{
void message()
{
System.out.println("This is base class method");
}
}
class derived extends base
{
void message()
{
System.out.println("This is derived class method");
}
void display()
{
// will invoke or call current class message() method
message();
// will invoke or call parent class message() method
super.message();
}
}
/* Driver program to test */
class Test
{
public static void main(String args[])
{
derived d = new derived();
d.display();
}
}
Output
This is Derived class method
This is Base class method
class base
{
base()
{
System.out.println("This is base class Constructor");
}
}
class derived extends base
{
derived()
{
super();
System.out.println("This is derived class Constructor");
}
}
class Test
{
public static void main(String[] args)
{
derived c = new derived();
}
}
Output:
base class Constructor
derived class Constructor
Abstract Method: A method which is declared as abstract and does not have implementation is known
as an abstract method.
abstract class A
{
void callmetoo()
class B extends A
void callme()
class Abstract_Demo
B b = new B();
b.callme();
b.callmetoo();
}
}
Output:
Example 2:
abstract class A
void callmetoo()
class B extends A
void callme()
class C extends A
{
void callme()
class Abstract_Demo
B b = new B();
C c = new C();
b.callme();
b.callmetoo();
c.callme();
c.callmetoo();
Output:
double dim1;
double dim2;
Figure(double a, double b)
dim1 = a;dim2 = b;
Rectangle(double a, double b)
super(a, b);
double area()
Triangle(double a, double b)
super(a, b);
double area() {
class Abstract_Areas
figref = r;
Output:
Differences Between Compile-time and Run-time
Polymorphism
There are two types of polymorphism in java:
1) Static Polymorphism also known as compile time polymorphism.
2) Dynamic Polymorphism also known as runtime polymorphism.
Compile Time Polymorphism:
Whenever an object is bound with their functionality at the compile-time, this is known as the
compile-time polymorphism.
At compile-time, java knows which method to call by checking the method signatures.
this is called compile-time polymorphism or static or early binding.
Compile-time polymorphism is achieved through method overloading.
Method Overloading says you can have more than one function with the same name in one class
having a different prototype. Function overloading is one of the ways to achieve polymorphism
but it depends on technology that which type of polymorphism we adopt. In java, we achieve
function overloading at compile-Time.
Run-Time Polymorphism:
Whenever an object is bound with the functionality at run time, this is known as runtime
polymorphism.
The runtime polymorphism can be achieved by method overriding.
Java virtual machine determines the proper method to call at the runtime, not at the compile
time.
It is also called dynamic or late binding.
Method overriding says child class has the same method as declared in the parent class. It
means if child class provides the specific implementation of the method that has been provided
by one of its parent class then it is known as method overriding.
Sr.No Compile Time Polymorphism Runtime Polymorphism
2 In this, the call is determined by the In this, the call is not determined by the
compiler. compiler.
3 The method is executed quite earlier at the The method is executed at the run-time,
compile-time, and that’s why it provides fast and that’s why it provides slow
execution. execution.
4 This polymorphism is also known as early This polymorphism is also known as late
binding, overloading, and static binding. binding, dynamic binding, and
overriding.
Example : upcasting
class A
class B extends A
A a:
B b=new B();
a=b; //upcasting
For upcasting, we can use the reference variable of class type.
Example:
class A
void callme()
class B extends A
// override callme()
void callme()
class C extends A
// override callme()
void callme()
}
}
class Dispatch
r = a; // r refers to an A object
r = b; // r refers to a B object
r = c; // r refers to a C object
Output:
final class :If you make any class as final, you cannot extend it.
Example:
final class base
{
}
class derived extends base
{
void run()
{
System.out.println("derived class running");
}
public static void main(String args[])
{
derived d= new derived();
d.run();
}
}
Output:
Blank or uninitialized final variable
o The blank final variable in Java is a final variable that is not initialized while declaration, instead
they are initialized in a constructor.
o A final variable that is not initialized at the time of declaration is known as blank final variable.
o If you want to create a variable that is initialized at the time of creating object and once
initialized may not be changed, it is useful.
class Test
{
final int i;
Test()
{
i = 10;
}
Void display()
{
System.out.println(i);
}
class demo
{
public static void main(String args[])
{
Test T1 = new Test();
T1.display();
}
}
Output
10