0% found this document useful (0 votes)
9 views

Unit 3

Uploaded by

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

Unit 3

Uploaded by

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

Unit 3

Arrays: Introduction, Declaration and Initialization of Arrays, Storage of Array in


Computer Memory, Accessing Elements of Arrays, Operations on Array Elements,
Assigning Array to Another Array, Dynamic Change of Array Size, Sorting of
Arrays, Search for Values in Arrays, Class Arrays, Two-dimensional Arrays, Arrays
of Varying Lengths, Three-dimensional Arrays, Arrays as Vectors.
Inheritance: Introduction, Process of Inheritance, Types of Inheritances,
Universal Super Class-Object Class, Inhibiting Inheritance of Class Using Final,
Access Control and Inheritance, Multilevel Inheritance, Application of Keyword
Super, Constructor Method and Inheritance, Method Overriding, Dynamic
Method Dispatch, Abstract Classes.
Interfaces: Introduction, Declaration of Interface, Implementation of Interface,
Multiple Interfaces, Nested Interfaces, Inheritance of Interfaces, Default
Methods in Interfaces, Static Methods in Interface.
Introduction
• Inheritance is the backbone of object-oriented
programming (OOP).
• It is the mechanism by which a class can acquire
properties and methods of another class.
• Using inheritance, an already tested and debugged
class program can be reused for some other
application.
Inheritance
• Inheritance can be defined as the process where one class acquires the properties
of another class
• The class which inherits the properties of other is known as subclass (derived class,
child class) and the class whose properties are inherited is known as superclass
(base class, parent class).
• Inheritance represents the IS-A relationship, also known as parent-child
relationship.
• Use
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
Syntax
class Subclass-name extends Superclass-name
{
//methods and fields
}
• The extends keyword indicates that you are
making a new class that derives from an
existing class.
• Super class This is the existing class from
which another class, that is, the subclass is
generally derived.
• In Java, several derived classes can have the
same super class.
• Subclass A class that is derived from another
class is called subclass.
• In Java, a subclass can have only one super class
Benefits of Inheritance
• It allows the reuse of already developed and debugged
class program without any modification.
• It allows a number of subclasses to fulfill the needs of
several subgroups.
• A large program may be divided into suitable classes and
subclasses that may be developed by separate teams of
programmers.
Process of Inheritance
• Inheritance means deriving some characteristics from something
that is generic.
• In the context of Java, it implies deriving a new class from an
existing old class, that is, the super class.
• A super class describes general characteristics of a class of
objects.
• A subset of these objects may have characteristics different from
others.
• There are two ways of dealing with this problem.
• Either, make a separate class for the subset to include all the
characteristics or, to have another class that inherits the existing
class, extend this class to include the special characteristics.

© Oxford University Press 2018. All rights reserved.


Process of Inheritance

© Oxford University Press 2018. All rights reserved.


Types of Inheritance
The following types of inheritances are supported by Java.

1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
4. Multiple inheritance using interfaces

© Oxford University Press 2018. All rights reserved.


Types of Inheritance
(i) Single inheritance: It is the simple type of inheritance. In this, a
class extends another one class only.

© Oxford University Press 2018. All rights reserved.


Types of Inheritances
(ii) Multilevel inheritance: In this type, a derived class inherits a parent
or super class;
• The derived class also acts as the parent class to other class.

© Oxford University Press 2018. All rights reserved.


Types of Inheritances
• Hierarchical inheritance: In this type, one class is inherited by
many sub classes.

© Oxford University Press 2018. All rights reserved.


Types of Inheritances
• Multiple inheritance: In this, a class is extending more than one
class.
• Java does not support multiple inheritance.
• This implies that a class cannot extend more than one class.
• Suppose there is a method in class A. This method is overridden in
class B and class C in their own way.
• Since class C extends both the classes A and B.
• So, if class C uses the same method, then there will be ambiguity
as which method is called.

© Oxford University Press 2018. All rights reserved.


Types of Inheritances

© Oxford University Press 2018. All rights reserved.


Types of Inheritances

© Oxford University Press 2018. All rights reserved.


Types of Inheritances
• A Super class reference variable can refer to a
subclass object.
• Also called as upcasting.
Universal Super Class—Object Class
• Object class is a special class and it is at the top of the class
hierarchy tree.
• It is the parent class or super class of all in Java.
• Hence, it is called Universal super class.
• Object is at the root of the tree and every other class can be
directly or indirectly derived from the Object class.

© Oxford University Press 2018. All rights reserved.


Universal Super Class—Object Class

The default implementation of


toString() returns a string that consists
of the class name followed by the "@"
character and the object's hash code in
hexadecimal form

© Oxford University Press 2018. All rights reserved.


Inehibiting Inheritance of Class using Final
• To prevent modifications to the instance variable
• To Prevent method overriding
• To prevent inheritance

• Ex: FinalVariable.java, FinalMethod.java, FinalClass.java


Use of super
• Whenever the derived class inherits the base
class features, there is a possibility that base
class features are similar to derived class
features and JVM gets an ambiguity.
• The super keyword in Java is a reference
variable that is used to refer immediate parent
class object.
• Usage of Java super Keyword
– super is used to refer immediate parent class instance
variable.
• super.parent_instance_variable_name;
– super is used to invoke immediate parent class method
• super.parent_class_method_name();
– super is used to invoke immediate parent class constructor.
• super(arglist);
• super(); //default

Super keyword is used to distinguish


between the variable/methods having
same name in super class and subclass.
Constructor Method and Inheritance
• Constructors of super class are not inherited by sub class but
the constructor of the super class can be invoked from sub
class using super keyword.
• Constructors are calling from bottom to top and executing
from top to bottom.
• To establish the connection between base class constructor
and derived class constructors JDK provides two methods
they are:
– super()
– super(arglist)
.
Super(...)

• Super(...) It is used for calling super class


parameterize constructor from the context of
derived class constructor.
• Important rules
- Whenever we are using either super() or super(...)
in the derived class constructors the super always
must be as a first executable statement in the body of
derived class constructor otherwise we get a
compile time error.
Access Control and Inheritance
• A derived class access to the members of a super class may
be modified with access specifiers.
• There are three access specifiers, that is, public, protected,
and private.
• The code for specifying access is
access-specifier type member_identifier;

A public class can inherit all the public and protected


members of super class. However, private members are
not directly accessible to sub class. These are
accessible with public methods of same class.
Method Overriding
• Method overriding is one of the ways in which polymorphism
can be implemented.
• Polymorphism – objects take different forms at different
environments.
• Method Overriding:
• When both super class and sub class contain a method
with same name and type signature - then the super
class method is overriden by the sub class method.
• When an overridden method is called from within its
subclass, it will always refer to the version of that
method defined by the subclass. The version of the
method defined by the superclass will be hidden.
Types of Binding
Connecting a method call to the method body is
known as binding.
There are two types of binding:
• Static binding / early binding /compile-time
• Dynamic binding /late /run-time
Static Binding (also known as Early 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.
• Dynamic Binding (also known as Late Binding).
• type of the object is determined at run-time
Dynamic Method Dispatch
• It is the mechanism by which run-time polymorphism is
achieved for overridden method in Java.
• It works on “ A Super class reference variable refers to a
sub class object”.
• Java uses this fact to resolve calls to overridden methods
at run time.
• Here is how?:
– When an overridden method is called through a
superclass reference, Java determines which version of
the method will be excuted based upon the type of the
object being referred.
Abstract method
• An abstract method is a method without definition.
• Syntax:
abstract return_type method_name(parameter_list);
Abstract class
• An abstract class is a class that contains one or more abstract
methods.
• An abstract class can contain instance variables, constructors,
concrete methods in addition to abstract methods.
• All the abstract methods of abstract class should be
implemented in its sub classes.
• If any abstract method is not implemented in its subclasses,
then that sub class must be declared as abstract.
• We can’t create an object to abstract class, but we can create
references to abstract class.
• Also, you cannot declare abstract constructors or abstract
static methods.
• QA?
– Can you declare a class as abstract and final?
Interfaces Introduction
• An interface represents an encapsulation of constants, classes,
interfaces, and one or more abstract methods that are
implemented by a class.
• An interface does not contain instance variables.
• An interface cannot implement itself; it has to be implemented by
a class.
• The methods in an interface have no body.
• Only headers are declared with the parameter list that is followed
by a semicolon.
Introduction
• The class that implements the interface has to have full
definitions of all the abstract methods in the interface.
• An interface can be implemented by any number of
unrelated classes with their own definitions of the
methods of the interface.
• Different classes can have different definitions of the
same methods but the parameter list must be identical
to that in the interface.
• Thus, interfaces provide another way of dynamic
polymorphic implementation of methods.
Introduction
• According to modifications in Java SE8, an interface can now have
default methods and static methods with full definitions
and these methods are inherited by the classes that implement
the interface.
• At the same time, the class may override the methods if
necessary.
• A class can implement any number of interfaces - fulfils the need
for multiple inheritance.
• The multiple inheritances of classes are not allowed in Java, and
therefore, interfaces provide a stopgap arrangement.

© Oxford University Press 2018. All rights reserved.


Similarities between Interface and Class
• Declaring an interface is similar to that of class; the keyword class
is replaced by keyword interface.
• Its accessibility can be controlled just like a class.
• An interface declared public is accessible to any class in any
package, whereas the ones without an access specifier is
accessible to classes in the same package only.
• Interface reference variables are allowed.
• It can contain inner classes (nested classes) and inner interfaces.
• Since Java 8, an interface can have full definitions of methods with
default or static modifiers.
Dissimilarities between Class and Interface
• Interface cannot implement itself; it must be implemented by a class.
• An interface can contain only method headers followed by a semicolon.
• It cannot have the full definition of a method. The full definition is given
in the class that implements it.
• The methods declared in the interface are implicitly public.
• An interface does not contain instance variables.
• The variables declared in an interface are implicitly public, static, and
final,
• Interfaces cannot have a constructor like a class.
• An interface cannot extend a class nor can it have a subclass. It can only
extend other interfaces.
• A class can extend (inherit) only one class but an interface can extend
any number of interfaces.
Rules for Classes that Implement Interface
• A non-abstract class that implements an interface must have
concrete implementation of all the abstract methods of the
interface.

• The @Override annotation is used on the definitions of


interface methods in the class.

• The methods declared static and default with full definitions in


an interface are inherited by the implementing class since Java
8.
Types of Interfaces
Top level interfaces
• It is an interface that is not nested in any class or interface.

Nested interface
• It is an interface that is defined in the body of a class or interface.
• It is referred to by the outer interface or class and cannot be accessed
directly.

class X{
interface y{}
}
class A implements X.y { }

© Oxford University Press 2018. All rights reserved.


Generic interface
• We can develop more adaptable and reusable interfaces
in Java since an interface can include a generic type
parameter.
• This is implemented for different data types.
public interface MyInterface<T> {
void doSomething(T value);
}
public class MyClass implements MyInterface<String> {
public void doSomething(String value) {
System.out.println("Doing something with " + value);
}
}
public class AnotherClass implements MyInterface<Integer> {
public void doSomething(Integer value) {
System.out.println("Doing something with " + value);
}
}

© Oxford University Press 2018. All rights reserved.


Declaration of Interface
• Declaration of an interface starts with the access modifier followed
by keyword interface.
• It is in then followed by its name or identifier that is followed by a
block of statements;
• These statements contain declarations of variables and abstract
methods.
• The variables defined in interfaces are implicitly public, static,
and final.
• They are initialized at the time of declaration.
• The methods declared in an interface are public by default.
• Since Java SE8, static and default methods with full definition
can also be members of interface.

© Oxford University Press 2018. All rights reserved.


Declaration of Interface

class classname [extends superclass]


[implements interface1 [,interface2...]] {
// class-body
}

© Oxford University Press 2018. All rights reserved.


Implementation of Interface
Syntax:
class classname [extends superclass]
[implements interface1 [,interface2...]] {
// class-body
}
Implementation of Interface
Declaration of class that implements an interface

If a class extends another class as well as implements interfaces,


it is declared as

© Oxford University Press 2018. All rights reserved.


Ex: FindAreas, use of constants
Multiple Interfaces
• Multiple interfaces can also be implemented in Java.
• For this, the class implements all the methods declared in all the interfaces.
• When the class is declared, names of all interfaces are listed after the keyword
implements and separated by comma.
• As for example, if class A implements interfaces C and D, it is defined as

© Oxford University Press 2018. All rights reserved.


Interface References
• In Java, an interface cannot be instantiated.

• Therefore, it cannot be referenced directly.

• However, an object of class type, which extends the

interface, can be used to assign a reference of that

interface type.

• InterfaceReferences.java
Nested Interfaces
• An interface may be declared as a member of a class or in another
• interface.
• In nested concepts interfaces can only be declared as public or with
default access.
• Syntax of nested interface in another interface is given as
Inheritance of Interfaces
• One interface can inherit another by use of the
keyword extends. The syntax is the same as like
inheriting classes.
• When a class implements an interface that inherits
another interface, it must provide implementations
for all methods defined within the interface
inheritance chain.
Default Methods in Interfaces
• Before Java 8, interfaces could
have only abstract methods.
• The implementation of these
methods has to be provided by
sub class.

• To overcome this issue, Java 8 has introduced the


concept of default methods which allow the interfaces
to have methods with implementation details.
• Default methods are also known as defender
methods or virtual extension methods.
• These methods can be overridden by implementing
classes.
Rules
• If a class implements two or more interfaces, the interfaces cannot
have default methods with the same signature because this would
cause ambiguity as to which one to execute.
• In the case of methods with the same name, the compiler would
choose by matching parameters.
• A default method cannot be declared final.
• If an interface A is extended by another interface B and both have
a default method with same name, the definition of interface B
will be chosen. Otherwise use super keyword.
• Ex:DefaultMethodEx3.java

© Oxford University Press 2018. All rights reserved.


Static Methods in Interface
• The Java version 8 allows full definition of static methods in
interfaces.
• The interfaces can have static methods which is similar to static
method of classes.
• Can be referred with interface names.
Interfacename.staticmethodname();
• NOTE: Static interface methods are not inherited by
either an implementing class or subinterface
• These methods can not be overridden by classes or interfaces.
Where as a default methods can be overridden.

© Oxford University Press 2018. All rights reserved.


Interface vs. Abstract Class
Abstract Class Interface
Contains some abstract methods Only abstract methods, default
and some concrete methods and static methods
Contains instance variables Only static and final variables
Doesn’t support multiple Supports
inheritance
public class Apple public class Person
extends Food { … } implements
Student,Athlete,Chef {
… }

56
Use of toString()
• The toString() method returns the String
representation of the object.
• If you print any object, Java compiler internally
invokes the toString() method on the object.
System.out.println(obj);
• So overriding the toString() method, returns
the desired output, it can be the state of an
object etc. depending on your implementation.

You might also like