JAVA UNIT-I-part-2
JAVA UNIT-I-part-2
INHERITANCE
INHERITANCE CONCEPT:
Inheritance in Java 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.
Advantages:
o For Method Overriding (so runtime polymorphism can be
achieved).
o For Code Reusability.
o
INHERITANCE BASICS:
Terms in Inheritance:
Class: A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
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.
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.
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.
Syntax:
Here is an example:
class Test {
int a; // default
access public int
b; // public access
private int c; //
private access //
methods to access
c
void setc(int i) { //
set c's value c = i;
}
int getc() { // get
c's value return
c;
CONSTRUCTORS
A constructor initializes an object immediately upon creation.
It has the same name as the class in which it resides and is
syntactically similar to a method.
Once defined, the constructor is automatically called
immediately after the object is created, before the new operator
completes.
Constructors have no return type, not even void. This is
because the implicit return type of a class‟ constructor is the
class type itself.
class
Box {
double
width;
double
height
;
double
depth;
double volume() {
return width *
height * depth; }
}
class BoxDemo {
public static void
main(String args[])
{ Box mybox1 = new
Box();
Box mybox2 =
new Box();
double vol;
vol = mybox1.volume();
System.out.println("Volume
is " + vol);
vol = mybox2.volume();
System.out.println("Volume
is " + vol); }
}
Parameterized Constructors
class
Box {
double
width;
double
height
;
double
depth;
}
class BoxDemo {
public static void
main(String args[]) { Box
mybox1 = new Box(10, 20,
15); Box mybox2 = new
Box(3, 6, 9); double vol;
vol = mybox1.volume();
System.out.println("Volume
is " + vol);
vol = mybox2.volume();
System.out.println("Volume
is " + vol); }
}
Example
class
ParentCla
ss{ int a;
ParentCla
ss(int a){
System.out.println("Inside ParentClass parameterized
constructor!"); this.a = a;
}
ParentClass(){
System.out.println("Inside ParentClass default constructor!");
Example
class
ParentCla
ss{ int
num = 10;
}
class ChildClass extends
ParentClass{ int num = 20;
Void showData() {
System.out.println("Inside the
ChildClass");
System.out.println("ChildClass num = " +
num); System.out.println("ParentClass
num = " + super.num);
}
}
public class
SuperKeywordExample {
public static void
main(String[] args) {
ChildClass obj = new
ChildClass();
obj.showData();
System.out.println("\nInside the non-child class");
Example
Class ParentClass{
Int num1 = 10;
Void ShowData()
{
Example
class
ParentCla
ss{ int
num1;
ParentCla
ss(){
System.out.println("\nInside the ParentClass default constructor");
num1 = 10;
}
ParentClass(int value){
num1 = value;
}
}
class ChildClass extends
ParentClass{ int num2;
ChildClas
s(){
super(100
);
System.out.println("\nInside the ChildClass constructor");
num2 = 200;
}
}
Public class SuperKeywordExample {
Public static void main (String[] args )
{
ChildClass obj = new ChildClass();
}
}
To call the parameterized constructor of the parent class, the super keyword
must be the first statement inside the child class constructor, and we must pass
the parameter values.
Class A
{
Final int n=10;
}
Class B extends A
{
Void show()
{
System.out.println(n++);//Error! Can’t be modified
}
}
class A {
final void meth() {
System.out.println("T
his is a final
method."); }
}
class B extends A {
JAVA PYLYMORPHISM
The polymorphism is the process of defining same method with
different implementation. That means creating multiple methods
with different behaviors.
In java, polymorphism implemented using method overloading and method
overriding.
Ad hoc polymorphism:
The ad hoc polymorphism is a technique used to define the same
method with different implementations and different arguments. In
a java programming language, ad hoc polymorphism carried out
with a method overloading concept.
In ad hoc polymorphism the method binding happens at the time of
compilation. Ad hoc polymorphism is also known as compile-time
polymorphism. Every function call binded with the respective
overloaded method based on the arguments.
The ad hoc polymorphism implemented within the class only.
import java.util.Arrays;
public class AdHocPolymorphismExample {
void sorting(int[] list) {
Arrays.parallelSort(list);
System.out.println("Integers after sort: " + Arrays.toString(list));
}
void sorting(String[] names) {
Arrays.parallelSort(names);
System.out.println("Names after sort: " + Arrays.toString(names));
}
public static void main(String[] args) {
AdHocPolymorphismExample obj = new AdHocPolymorphismExample();
int list[] = {2, 3, 1, 5, 4};
obj.sorting(list); // Calling with integer array
Pure Polymorphism:
The pure polymorphism is a technique used to define the same
method with the same arguments but different implementations. In
a java programming language, pure polymorphism carried out with
a method overriding concept.
In pure polymorphism, the method binding happens at run time.
Pure polymorphism is also known as run-time polymorphism.
Every function call binding with the respective overridden method
based on the object reference.
When a child class has a definition for a member function of the
parent class, the parent class function is said to be overridden.
The pure polymorphism implemented in the
inheritance concept only.
void showData() {
System.out.println("Inside ChildClass showData() method");
System.out.println("num = " + num);
}
public class PurePolymorphism {
public static void main(String[] args) {
ParentClass obj = new ParentClass();
obj.showData();
obj = new ChildClass();
obj.showData();
}
}
class ParentClass{
void showData() {
obj.showData();
radius = input.nextInt();
radius);
rec.printArea();
Triangle tri = new Triangle();
tri.printArea();
Cricle cri = new Cricle();
cri.printArea();
}
}
Method Purpose
void wait( )
void wait(long milliseconds)
Waits on another thread of execution.
void wait(long milliseconds, int
nanoseconds)
FORMS OF INHERITANCE
Subclass, Subtype, Substitutability:
Substitutability:
Note: Subtypes must satisfy the substitution principle, which means that
[67] Syeda Sumaiya
Afreen
R 20 OOPS THROUGH JAVA
any code that relies on B’s specification will function properly if A’s
implementation is substituted for B.
o Specialization
o Specification
o Construction
o Generalization or Extension
o Limitation
o Combination
1. Specialization Inheritance
✓ Each child class overrides a method inherited from the parent in order
Example:
class First
{
void show()
{
System.out.println(―Hello‖);
}
void showA()
{
System.out.println(―Welcome‖);
}
}
2. Specification Inheritance
✓ If the parent class is used as a source for behavior, but the child class has
no is-a relationship to the parent, then we say the child class is using
inheritance for construction
class First
{
void show()
[70] Syeda Sumaiya
Afreen
R 20 OOPS THROUGH JAVA
{
System.out.println(―Hello‖);
}
void showA()
{
System.out.println(―Welcome‖);
}
}
void add()
{
..........
........... }
more
functionality, but does not override any method, we call it inheritance for
generalization
class First
{
void set(int a, int b)
{
.........
.........
}
void add()
{
..........
...........
}
✓ If a child class overrides a method inherited from the parent in a way that
✓ Generally not a good idea, since it breaks the idea of substitution. But
class First
{
void set(int a, int b)
{// initialization}
void add()
{
..........
...........
}
The child class inherits features from more than one parent class. Although
multiple inheritance is not supported directly by Java, it can be achieved using
interfaces.
[73] Syeda Sumaiya
Afreen
R 20 OOPS THROUGH JAVA
interface A
{
……
}
interface B
{
…….
}
class C implements A, B
{
……..
}
Specialization
The child class is a special case of the parent class, in other words, the
child class is a subtype of the parent class.
Specification
The parent class defines behaviour that is implemented in the child class but
not in the parent class.
Construction
The child class makes use of the behaviour provided by the parent class, but is
Generalizaton
The child class modifies or overrides some of the methods of the parent
class.
Extension
The child class adds new functionality to the parent class, but does not
change any inherited behavior.
Limitation
The child class restricts the use of some of the behavior inherited from the
parent class.
Combination
The child class inherits features from more than one parent class. This is
multiple inheritance.
BENEFITS OF INHERITANCE
• Inheritance helps in code reuse. The child class may use the
code defined in the parent class without re-writing it.
• Inheritance can save time and effort as the main code need not
be written again.
• Inheritance provides a clear model structure which is easy to
understand.
• An inheritance leads to less development and maintenance
costs.
• With inheritance, we will be able to override the methods of
the base class so that the meaningful implementation of the
base class method can be designed in the derived class. An
COSTS OF INHERITANCE