0% found this document useful (0 votes)
24 views26 pages

Inheritance, Interface and Packages

Chapter 3 covers key concepts in Java programming, including inheritance, interfaces, abstract classes, and packages. It explains how classes can inherit characteristics from parent classes, the use of the super keyword, method overriding and overloading, and the distinction between abstract classes and interfaces. Additionally, it discusses the role of packages in organizing classes and preventing naming conflicts.

Uploaded by

kkp002003
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)
24 views26 pages

Inheritance, Interface and Packages

Chapter 3 covers key concepts in Java programming, including inheritance, interfaces, abstract classes, and packages. It explains how classes can inherit characteristics from parent classes, the use of the super keyword, method overriding and overloading, and the distinction between abstract classes and interfaces. Additionally, it discusses the role of packages in organizing classes and preventing naming conflicts.

Uploaded by

kkp002003
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/ 26

Chapter 3

INHERITANCE, INTERFACE AND


PACKAGES
INHERITANCE
 derive a new class from an existing one
 The existing class is called the parent class,
or superclass, or base class
 The derived class is called the child class or
subclass.
 As the name implies, the child inherits
characteristics of the parent
 That is, the child class inherits the methods
and data defined for the parent class
Parent class/Super
 Vehicle
class

 Car Child class / sub class

class Car extends Vehicle


{
// class contents of
vehical
}
Book

# pages : int

+ pageMessage() : void

Dictionary
Words
- definitions : int
+ main (args :
String[]) : void + definitionMessage() :
void
SUPER KEYWORD
 Constructors are not inherited, even though
they have public visibility
 Yet we often want to use the parent's
constructor to set up the "parent's part" of
the object
 The super reference can be used to refer to
the parent class, and often is used to invoke
the parent's constructor
 A child’s constructor is responsible for calling
the parent’s constructor
 The first line of a child’s constructor should
use the super reference to call the parent’s
constructor
 The super reference can also be used to
reference other variables and methods
defined in the parent’s class
 Multiple inheritance allows a class to be
derived from two or more classes, inheriting
the members of all parents
 Collisions, such as the same variable name in
two parents, have to be resolved
 Java does not support multiple inheritance
 In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
OVERRIDE
 A child class can override the definition of an
inherited method in favor of its own
 The new method must have the same
signature as the parent's method, but can
have a different body
 The type of the object executing the method
determines which version of the method is
invoked
 A parent method can be invoked explicitly using
the super reference
 If a method is declared with the final modifier, it
cannot be overridden
 The concept of overriding can be applied to data
and is called shadowing variables
 Shadowing variables should be avoided because it
tends to cause unnecessarily confusing code
OVERLOADING
Overloading
VS OVERRIDING
Overriding
multiple methods with the same name two methods, one in a parent class and
in the same class, but with different one in a child class, that have the same
signatures signature

define a similar operation in different define a similar operation in different


ways for different data ways for different object types

compile-time polymorphism Run-time polymorphism

It occurs within the class. It is performed in two classes with


may or may not require inheritance. inheritance relationships.
always needs inheritance.

Static binding is being used for Dynamic binding is being used for
overloaded methods. overriding methods.

The return type can or can not be the The return type must be the same or co-
same, but we just have to change the variant.
parameter

The argument list should be different The argument list should be the same in
while doing method overloading. method overriding.
ABSTRACT CLASSES
 An abstract class is a placeholder in a class
hierarchy that represents a generic concept
 An abstract class cannot be instantiated
 We use the modifier abstract on the class
header to declare a class as abstract:
 public abstract class Whatever
{
// contents
}
 contains abstract methods with no definitions
(like an interface does)
 Modifier must be applied to each abstract
method
 Also contains non-abstract methods (with
bodies), further distinguishing abstract
classes from interfaces
 A class declared as abstract does not need to
contain abstract methods
 The child of an abstract class must override
the abstract methods of the parent, or it too
will be considered abstract
 An abstract method cannot be defined as
final (because it must be overridden) or
static (because it has no definition yet)
 The use of abstract classes is a design
decision – it helps us establish common
elements in a class that is too general to
instantiate
INTERFACES
 An interface in Java is a blueprint of a
class. It has static constants and
abstract methods
 The interface in Java is a mechanism to

achieve abstraction. T
 here can be only abstract methods in the

Java interface.
 It is used to achieve abstraction and multiple

inheritance in Java.
 represents the IS-A relationship.
 It cannot be instantiated just like the abstract

class.
 we can have default and static methods in an

interface, private methods in an interface


 It is used to achieve abstraction.

 support the functionality of multiple inheritance.

 achieve loose coupling


DIFFERENCE BETWEEN ABSTRACT CLASS
AND INTERFACE
FINAL KEYWORD
 Final variable
A final variable is a variable whose
value cannot be changed at anytime once
assigned, it remains as a constant forever.
 Final method

When you declare a method as final,


then it is called as final method. A final
method cannot be overridden.
 Final class

A final class cannot be extended,


cannot be sub classed
INNER CLASSES
 Inner class means one class which is a
member of another class. There are basically
four types of inner classes in java.
 Nested Inner class
 Method Local inner classes

 Anonymous inner classes

 Static nested classes


 Nested Inner class
 Nested Inner class can access any
private instance variable of outer class.

class Outer {
// Simple nested inner class
public void show() {
System.out.println("In a nested class method");
}
}
}
class Main {
public static void main(String[] args) {
Outer.Inner in = new Outer().new Inner();
in.show();
}
}
Output:
In a nested class method ass class Inner
Method Local inner classes
 Inner class can be declared within a method of an outer class.

class Outer {
void outerMethod() {
System.out.println("inside outerMethod"); // Inner class is local to outerMethod()
class Inner {
void innerMethod() {
System.out.println("inside innerMethod");
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class MethodDemo {
public static void main(String[] args) {
Outer x = new Outer(); x.outerMethod();
}
}
Output:
Inside outerMethod Inside innerMethod
 Static nested classes
Static nested classes are not technically an inner class. They are like
a static member of outer

Example:
class Outer {
private static void outerMethod() {
System.out.println("inside outerMethod");
}
// A static inner class static class Inner {
public static void main(String[] args) {
System.out.println("inside inner class Method");
outerMethod();
}
}
}
Output:
inside inner class Method inside outerMethod
 Anonymous inner classes
Anonymous inner classes are declared
without any name at all. They are created in
two ways.
PACKAGES IN JAVA

 Package in Java is a mechanism to


encapsulate a group of classes, sub packages
and interfaces.
 Preventing naming conflicts.
 college.staff.ee.Employee
 college.staff.cse.Employee

 Making searching/locating and usage of classes,


interfaces
 Providing controlled access
 Packages can be considered as data
encapsulation (or data-hiding).
 Package naming conventions
 Adding a class to a Package

 Subpackages
 import java.util.*;

You might also like