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

Java Inheritance and Interfaces Guide

Uploaded by

23b113
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)
4 views72 pages

Java Inheritance and Interfaces Guide

Uploaded by

23b113
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

Unit 2 - INHERITANCE, PACKAGES AND INTERFACES

Overloading Methods – Objects as Parameters – Returning


Objects –Static, Nested and Inner Classes. Inheritance:
Basics– Types of Inheritance -Super keyword -Method
Overriding – Dynamic Method Dispatch –Abstract Classes –
final with Inheritance. Packages and Interfaces: Packages –
Packages and Member Access –Importing Packages –
Interfaces.
Overloading Methods
Method Overloading
● In Java, it is possible to define two or more methods within
the same class that share the same name, as long as their
parameter declarations are different.
● methods are said to be overloaded, and the process is
referred to as method overloading.
● One way to implement polymorphism
● When a method is called, java matches up with the method
name first and then the number and type of parameters to
decide which function has to be called.
Objects as Parameters
Objects as Parameters
● to pass objects to methods
● most common uses of object parameters involves constructors.
Returning Objects
Returning Objects
● A method can return any type of data, including class types that
you create.
Returning Objects
class RetOb {
public static void main(String args[]) {
// Returning an object. Test ob1 = new Test(2);
class Test { Test ob2;
int a; ob2 = [Link]();
Test(int i) { [Link]("ob1.a: " + ob1.a);
a = i; [Link]("ob2.a: " + ob2.a);
} ob2 = [Link]();
Test incrByTen() { [Link]("ob2.a after second
Test temp = new Test(a+10);
increase: "+ ob2.a);
return temp; }
} }
}
Static, Nested and Inner Classes
Static, Nested and Inner Classes
● define a class within another class
● scope of a nested class is bounded by the scope of its enclosing class.
● nested class has access to the members, including private members, of
the class in which it is nested.
● enclosing class does not have access to the members of the nested class.
● two types of nested classes:
○ static and non-static.
Nested and
Inner Classes
Static Nested Class
● Static -
○ has the static modifier
applied.
○ cannot directly access non-
static members
○ Syntax

[Link] = new
[Link]
Non Static Nested Class
● Non Static -
○ can directly access both
static and non-static
members
○ Syntax
○ Create object for Outer
class and create for Inner
class
Inheritance
● Reusing the properties of existing ones
● Inheritance is a mechanism in which
one object acquires all the properties
and behaviors of a parent object.
● Idea-
● create new classes that are built upon
existing classes
● When a child class inherits the
properties of a parent class, it is called
inheritance “you look exactly like your
● IS-A relationship father/mother”

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 20


Parent class and child class

New Derives from Existin


Clas g
s Class

Derived Class
Child Class Parent Class
Sub Class Base Class

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 21


Inheritance

Common
attributes
moved to
parent class
Two classes
have more or
less same set
of attributes

Child classes keep only attribute and methods


relevant to them

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 22


Defining a subclass
▪ Keyword ‘extends’ implies properties of superclass are extended by
subclass
▪ Subclass will contain its own methods and variables along with
superclass methods and variables.
class Subclass-
name extends Superclass-name
{
//methods and fields
}

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 23


Types of Inheritance

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 24


Single Inheritance
▪ refers to a child and parent class relationship where a
class extends the another class.

* JAVA PROGRAMMING - [Link] 25


Multilevel Inheritance
▪ a class extends to another class that is already
extended from another class
▪ Eg
▪ class Vehicle, class Car, and class SUV
▪ class Vehicle is the grandfather class. The
class Car extends class Vehicle and the class
SUV extends class Car

* JAVA PROGRAMMING - [Link] 26


Multiple Inheritance
▪ Defining derived class from numerous
base classes is known as ‘Multiple
Inheritance’
▪ Multiple inheritances are available in
object-oriented programming with C++,
but it is not available in Java.
▪ Java developers want to use multiple
inheritances in some cases.
▪ Java developers have interface concepts
expecting the developers to achieve
multiple inheritances by using multiple
* JAVA PROGRAMMING - [Link] 27
Hierarchical Inheritance
▪ more than one derived class extends a single
base class

▪ Ex

▪ parent class Car

▪ child classes Audi, BMW and Mercedes

* JAVA PROGRAMMING - [Link] 28


Method Overriding
▪ Subclasses access methods of superclass

▪ Method inheritance allows to use methods by subclass and need not


be define in subclass

▪ Some situation – when same method in both parent and child but
need different behaviour.

▪ Possible by Method overriding

▪ If methods with the same name are defined in both superclass and
subclass, the method in the subclass overrides the method in the
superclass. This is called method overriding.
* JAVA PROGRAMMING - [Link] 29
* JAVA PROGRAMMING - [Link] 30
Super
▪ reference variable which is used to refer immediate
parent class object.

▪ used in subclasses to access superclass members


(attributes, constructors and methods).

▪ call methods of the superclass that is overridden in the


subclass.

▪ access attributes (fields) of the superclass if both


superclass and subclass have attributes with the same
name.

▪ explicitly
* call superclass no-arg (default) or - [Link]
JAVA PROGRAMMING 31
* JAVA PROGRAMMING - [Link] 32
Abstraction

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 33


Abstractio
Placing an order for
n food
Abstraction
⮚ abstraction is a process of hiding the
implementation details from the user, only
the functionality will be provided to the
user

⮚ user will have the information on what the


object does instead of how it does it.

⮚ In Java, abstraction is achieved using


Abstract classes and interfaces.
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 35
Abstract Class

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 36


Abstract Class
⮚ Keyword : abstract

⮚ contains abstract methods -- methods without body

⮚ Any class with one or more abstract method should be declared


abstract

⮚ Also can have concrete method

⮚ Abstract class cannot be instantiated

⮚ Defines a generalized form shared by all of its subclass. each


* subclass can provide specific implementations
20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 37
Cannot be
Abstract Class implemente
d in parent
class

Class
Animal Method
called
Sound()

Sub Lion class will say “Roar” in this


method and Dog class will say “Woof
class
Abstract Class
//abstract parent class
abstract class Animal public static void main(String args[])
{ {
//abstract method Animal obj = new Dog();
public abstract void sound(); [Link]();
} }
//Dog class extends Animal class }
public class Dog extends Animal
{
public void sound() making this method abstract we force all the
sub classes to implement this method
{
[Link]("Woof");
} Doesn’t provide full abstraction
Abstract Method
⮚ method declaration with no definition

⮚ Abstract method in superclass has to be overridden by all its


subclass

⮚ A normal subclass must override all abstract methods available in


Parent class

⮚ or subclass must be declared as abstract

⮚ This method does not have body


* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 40
Abstract Method
Clas
s

SubClass SubClass

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 41


Abstract Method
abstract type name(parameter-
list);

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 42


Assignment Problem

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 43


Interfaces

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 44


Interfaces
▪ Inheritance – single class cannot inherit multiple classes

Not permitted in
java

▪ To use properties and methods from distinct classes, java uses


interface concept

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 45


Interfaces
⮚ another way to achieve abstraction
⮚ An interface is a fully abstract class.
⮚ It includes a group of abstract methods
Booking a
Cab

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 46


Interface
⮚ Keyword – Interface
⮚ An interface is collection of abstract methods and final variables
⮚ A class implements interface using implements clause
⮚ All methods are by default public & abstract
⮚ All variables declared interface are public, static & final
⮚ cannot be instantiated

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 47


Final

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 48


Final
⮚ used for behavioral restriction of
⮚ variables
⮚ methods
⮚ classes
⮚ Final on variables – makes them as constants

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 49


Final
⮚ Final on methods
⮚ prevent overriding
⮚ prevent certain methods in
superclass from being overridden by
its subclass

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 50


Final
⮚ Final on class(inheritance)
⮚ prevent a class from being
inherited
⮚ precede the class declaration with
final
⮚ makes all its methods too final
⮚ illegal to declare a class as both
abstract and final

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 51


Quiz

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 52


Why Interfaces are required?
⮚ allows you to implement common behavior
⮚ specifies behaviours common to several kinds of objects
⮚ Any change in one class does not affect other class.
⮚ A class can implement more than one interface by giving comma
separated list
⮚ Java does not support multiple inheritance. interface will be
equivalent to multiple inheritance
⮚ reduces coupling between components in your software.

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 53


* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 54
* JAVA PROGRAMMING - [Link] 55
Defining & Implementing an Interface

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 56


Implementing Multiple Inheritance
interface A {
When a child class inherits from multiple
// members of A
} parent classes, it is called multiple
inheritance
interface B {
// members of B
}

class C implements A, B
{
// abstract members of
A
// abstract members of
B
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 57
interface X
{
public void myMethod();
}
interface Y
{
public void myMethod();
}
class JavaExample implements X, Y
{
public void myMethod()
{
[Link]("Implementing more than one interfaces");
}
public static void main(String args[])
{
JavaExample obj = new JavaExample();
[Link]();
}
}
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 58
Key Facts
Extending Interface
⮚ one interface can extend one or more interfaces using keyword
extends

interface Line {
// members of Line interface
}
// extending interface
interface Polygon extends Line {
// members of Polygon interface
// members of Line interface
}

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 60


* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 61
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 62
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 63
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 64
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 65
* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 66
Packages

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 67


Packages
▪ A java package is a group of similar types of classes, interfaces
and sub-packages.
▪ Two form
▪ built-in package - package we create

▪ user-defined package - already defined package

▪ used to categorize the classes and interfaces so that they can be


easily maintained.
▪ provides access protection

▪ removes naming collision

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 68


Packages

import [Link]

▪ Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 69


How to access package from another
package?
⮚ There are three ways to access the package from outside the
package.
1. import package.*;
Eg: import pack.*;
2. import [Link];
Eg: import pack.A;
3. fully qualified name.
Eg: pack.A obj = new pack.A();

* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 70


* 20IT202 – OBJECT ORIENTED PROGRAMMING CONCEPTS - [Link] 71
* JAVA PROGRAMMING - [Link] 72

You might also like