Unit II Inheritance ,Interface
and Packages
Inheritance in Java
• Java, Inheritance is an important pillar of OOP(Object-
Oriented Programming).
• It is the mechanism in Java by which one class is allowed to
inherit the features(fields and methods) of another class.
• In Java, Inheritance means creating new classes based on
existing ones.
• A class that inherits from another class can reuse the
methods and fields of that class.
Java Inheritance Types
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Multiple Inheritance
• 1. Single Inheritance
In single inheritance, a sub-class is derived from only one super
class.
It inherits the properties and behavior of a single-parent class.
Sometimes, it is also known as simple inheritance.
In the below figure, ‘A’ is a parent class and ‘B’ is a child class. The
class ‘B’ inherits all the properties of the class ‘A’.
2. Multilevel Inheritance
• In Multilevel Inheritance, a derived class will be inheriting a base class, and
as well as the derived class also acts as the base class for other classes.
• 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.
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 classes B, C, and
D
• 4. Multiple Inheritance (Through Interfaces)
In and inherit features from all parent classes. Please note that
Java does not support multiple inheritances with classes. In Java,
we can achieve multiple inheritances only through InterMultiple
inheritances, one class can have more than one superclass faces
.
Method Overriding
• Overriding in Java occurs when a subclass implements a
method which is already defined in the superclass or Base
Class.
• The method in the subclass must have the same signature as
inherited methods in the superclass. It allows the subclass to
modify the inherited methods.
Method overriding is a key concept in Java that enables
Runtime polymorphism.
It allows a subclass to provide its specific implementation for
a method inherited from its parent class.
The actual method executed is determined by the object’s
runtime type, not just the reference variable’s type.
This dynamic behaviour is crucial for creating flexible and
extensible object-oriented designs.
final Keyword in Java
• The final Keyword in Java is used as a non-access
modifier applicable only to a variable, a method, or
a class. It is used to restrict a user in Java.
• The following are different contexts where the final
is used:
• Variable
• Method
• Class
• Final variables: When a variable is declared as
final, its value cannot be changed once it has been
initialized. This is useful for declaring constants or
other values that should not be modified.
• Final methods: When a method is declared as
final, it cannot be overridden by a subclass. This is
useful for methods that are part of a class’s public
API and should not be modified by subclasses.
Abstract Methods and Classes
• An abstract class is a class that is declared abstract—it
may or may not include abstract methods. Abstract
classes cannot be instantiated, but they can be
subclassed.
• An abstract method is a method that is declared
without an implementation (without braces, and
followed by a semicolon)
• When an abstract class is subclassed, the subclass
usually provides implementations for all of the abstract
methods in its parent class. However, if it does not,
then the subclass must also be declared abstract.
Interfaces and Packages
• An interface defines a protocol of behavior that can be
implemented by any class anywhere in the class
hierarchy.
• An interface defines a set of methods but does not
implement them. A class that implements the
interface agrees to implement all the methods defined
in the interface, thereby agreeing to certain behavior.
• An interface is a named collection of method
definitions (without implementations).
• Interface reserve behaviors for classes that implement
them.
• Methods declared in an interface are always public
and abstract, therefore Java compiler will not
complain if you omit both keywords
• Static methods cannot be declared in the interfaces
– these methods are never abstract and do not
express behavior of objects
• Variables can be declared in the interfaces. They can
only be declared as static and final. – Both keyword
are assumed by default and can be safely omitted.
• Sometimes interfaces declare only constants – be
used to effectively import sets of related constants.
Defining an Interface
• Defining an interface is similar to creating a new class.
• An interface definition has two components: the
interface declaration and the interface body.
interfaceDeclaration
{
interfaceBody
}
– The interfaceDeclaration declares various attributes about the
interface such as its name and whether it extends another
interface.
– The interfaceBody contains the constant and method
declarations within the interface
public interface StockWatcher
{
final String sunTicker = "SUNW";
final String oracleTicker = "ORCL";
final String ciscoTicker = "CSCO";
void valueChanged
(String tickerSymbol,
double newValue);
}
If you do not specify that your interface is public, your
interface will be accessible only to classes that are defined in the
same package as the interface
Implementing an Interface
• Include an implements clause in the class
declaration.
• A class can implement more than one interface (the
Java platform supports multiple inheritance for
interfaces), so the implements keyword is followed
by a comma-separated list of the interfaces
implemented by the class.
• When implement an interface, either the class must
implement all the methods declared in the interface
and its superinterfaces, or the class must be declared
abstract
• What is Package?
A package is a collection of related classes and interfaces
providing access protection and namespace management.
To make classes easier to find and to use ,To avoid naming
conflicts ,To control access.
Programmers can easily determine that these classes and
interfaces are related.
Programmers know where to find classes and interfaces that
provide graphics-related functions.
The names of classes won’t conflict with class names in other
packages, because the package creates a new namespace.
Types of Java Packages
• 1. Built-in Packages
• These packages consist of a large number of classes which are a
part of Java API.Some of the commonly used built-in packages are:
• java.lang: Contains language support classes(e.g classes which
defines primitive data types, math operations). This package is
automatically imported.
• java.io: Contains classes for supporting input / output operations.
• java.util: Contains utility classes which implement data structures
like Linked List, Dictionary and support ; for Date / Time
operations.
• java.applet: Contains classes for creating Applets.
• java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc). 6)
• 2. User-defined Packages
• These are the packages that are defined by the
user.
• 1. Create the Package:
• First we create a directory myPackage (name
should be same as the name of the package).
Then create the MyClass inside the directory with
the first statement being the package names

Unit II Inheritance ,Interface and Packages.pptx

  • 1.
    Unit II Inheritance,Interface and Packages
  • 2.
    Inheritance in Java •Java, Inheritance is an important pillar of OOP(Object- Oriented Programming). • It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. • In Java, Inheritance means creating new classes based on existing ones. • A class that inherits from another class can reuse the methods and fields of that class.
  • 3.
    Java Inheritance Types •Single Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Multiple Inheritance
  • 4.
    • 1. SingleInheritance In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance. In the below figure, ‘A’ is a parent class and ‘B’ is a child class. The class ‘B’ inherits all the properties of the class ‘A’. 2. Multilevel Inheritance • In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. • 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.
  • 5.
    3. Hierarchical Inheritance InHierarchical 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 classes B, C, and D • 4. Multiple Inheritance (Through Interfaces) In and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through InterMultiple inheritances, one class can have more than one superclass faces .
  • 6.
    Method Overriding • Overridingin Java occurs when a subclass implements a method which is already defined in the superclass or Base Class. • The method in the subclass must have the same signature as inherited methods in the superclass. It allows the subclass to modify the inherited methods.
  • 7.
    Method overriding isa key concept in Java that enables Runtime polymorphism. It allows a subclass to provide its specific implementation for a method inherited from its parent class. The actual method executed is determined by the object’s runtime type, not just the reference variable’s type. This dynamic behaviour is crucial for creating flexible and extensible object-oriented designs.
  • 8.
    final Keyword inJava • The final Keyword in Java is used as a non-access modifier applicable only to a variable, a method, or a class. It is used to restrict a user in Java. • The following are different contexts where the final is used: • Variable • Method • Class
  • 9.
    • Final variables:When a variable is declared as final, its value cannot be changed once it has been initialized. This is useful for declaring constants or other values that should not be modified. • Final methods: When a method is declared as final, it cannot be overridden by a subclass. This is useful for methods that are part of a class’s public API and should not be modified by subclasses.
  • 10.
    Abstract Methods andClasses • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon) • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  • 11.
    Interfaces and Packages •An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. • An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior. • An interface is a named collection of method definitions (without implementations). • Interface reserve behaviors for classes that implement them.
  • 12.
    • Methods declaredin an interface are always public and abstract, therefore Java compiler will not complain if you omit both keywords • Static methods cannot be declared in the interfaces – these methods are never abstract and do not express behavior of objects • Variables can be declared in the interfaces. They can only be declared as static and final. – Both keyword are assumed by default and can be safely omitted. • Sometimes interfaces declare only constants – be used to effectively import sets of related constants.
  • 13.
    Defining an Interface •Defining an interface is similar to creating a new class. • An interface definition has two components: the interface declaration and the interface body. interfaceDeclaration { interfaceBody } – The interfaceDeclaration declares various attributes about the interface such as its name and whether it extends another interface. – The interfaceBody contains the constant and method declarations within the interface
  • 14.
    public interface StockWatcher { finalString sunTicker = "SUNW"; final String oracleTicker = "ORCL"; final String ciscoTicker = "CSCO"; void valueChanged (String tickerSymbol, double newValue); } If you do not specify that your interface is public, your interface will be accessible only to classes that are defined in the same package as the interface
  • 15.
    Implementing an Interface •Include an implements clause in the class declaration. • A class can implement more than one interface (the Java platform supports multiple inheritance for interfaces), so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. • When implement an interface, either the class must implement all the methods declared in the interface and its superinterfaces, or the class must be declared abstract
  • 16.
    • What isPackage? A package is a collection of related classes and interfaces providing access protection and namespace management. To make classes easier to find and to use ,To avoid naming conflicts ,To control access. Programmers can easily determine that these classes and interfaces are related. Programmers know where to find classes and interfaces that provide graphics-related functions. The names of classes won’t conflict with class names in other packages, because the package creates a new namespace.
  • 17.
    Types of JavaPackages • 1. Built-in Packages • These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are: • java.lang: Contains language support classes(e.g classes which defines primitive data types, math operations). This package is automatically imported. • java.io: Contains classes for supporting input / output operations. • java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations. • java.applet: Contains classes for creating Applets. • java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc). 6)
  • 18.
    • 2. User-definedPackages • These are the packages that are defined by the user. • 1. Create the Package: • First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names

Editor's Notes

  • #16 A package is a collection of related classes and interfaces providing access protection and namespace management. To make classes easier to find and to use To avoid naming conflicts To control access A package is a collection of related classes and interfaces providing access protection and namespace management. To make classes easier to find and to use To avoid naming conflicts To control access