Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Java Programming Module 2_Packages and Inheritance-1

The document discusses Java packages and inheritance, explaining how packages encapsulate classes and interfaces to prevent naming conflicts and provide controlled access. It details built-in and user-defined packages, as well as various types of inheritance in Java, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance. Additionally, it highlights the advantages and disadvantages of inheritance, emphasizing code reusability, abstraction, and potential complexity.

Uploaded by

halaplay385
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Programming Module 2_Packages and Inheritance-1

The document discusses Java packages and inheritance, explaining how packages encapsulate classes and interfaces to prevent naming conflicts and provide controlled access. It details built-in and user-defined packages, as well as various types of inheritance in Java, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance. Additionally, it highlights the advantages and disadvantages of inheritance, emphasizing code reusability, abstraction, and potential complexity.

Uploaded by

halaplay385
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Department of SCSE

Class B.Tech. (CSE) Sem-III

Course Title: Java Programming

Course Code: E1UA307C

Unit-2
Java Packages & API
• In Java, Package is a mechanism to encapsulate a group of classes, sub packages and interfaces.
OR
• A java package is a group of similar types of classes, interfaces and sub-packages.
Packages are used for:
• Preventing naming conflicts:
For example there can be two classes with name Employee in two packages:
college.staff.cse.Employee
college.staff.ee.Employee
• Providing controlled access: Packages have protected and default level access control.
A protected member is accessible by classes in the same package and its subclasses.
A default member (without any access specifier) is accessible by classes in the same package
only.
• Think of it as a folder in a file directory.
• It makes searching and usage of classes, interfaces, enumerations and annotations easier.
Adding a class to a Package : We can add more classes to a created package by using package name at
the top of the program and saving it in the same package directory.

Subpackages: Packages that are inside another package are the subpackages.
• These are not imported by default, they have to imported explicitly.
• Also, members of a subpackage have no access privileges, i.e., they are considered as different
package for protected and default access specifiers.

• Example :
import java.util.*;
util is a subpackage created inside java package.

Accessing classes inside a package: Consider following two statements :


import java.util. Scanner; // import the Scanner class from util package.
import java.util.*; // import all the classes from util package
Types of packages: Packages are divided into two categories:
1. Built-in Packages (packages from the Java API)
2. User-defined Packages (create your own packages)

3. 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:
1. java.lang: Contains language support classes (e.g classes which defines primitive data types, math
operations). This package is automatically imported.
2. java.io: Contains classes for supporting input / output operations.
3. java.util: Contains utility classes which implement data structures like Linked List, Dictionary
and support ; for Date / Time operations.
4. java.applet: Contains classes for creating Applets.
5. java.awt: Contain classes for implementing the components for graphical user interfaces (like
button , ;menus etc).
6. java.net: Contain classes for supporting networking operations.
2. User-defined packages: These are the packages that are defined by the user.

• 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.

• run java package program: You need to use fully qualified name e.g. mypack.Simple etc to run
the class.

To Compile: javac -d . Simple.java

To Run: java mypack.Simple

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination.
The “ . ” represents the current folder.
• Example
package myPackage;
public class MyClass {
public void getNames(String s) {
System.out.println(s);
} }
Now we can use the MyClass class in our program:
import myPackage.MyClass;
public class PrintName {
public static void main(String
args[]) {
String name = “Hello Students";
MyClass obj = new MyClass();
obj.getNames(name);
} }
Access package from another package
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name. Ex. pack.classname obj = new pack.classname ();
Inheritance
• Inheritance is an important pillar of OOP(Object-Oriented Programming).
• It is the mechanism 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 class.
• A class that inherits from another class can reuse the methods and fields of that class. In
addition, we can add new fields and methods to our current class as well.

• subclass (child) - The class whose features are inherited is known as a superclass(or a
base class or a parent class).
• superclass (parent) - The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.

9
Need of Inheritance:
• Code Reusability: Code written in the Parent class is common to all its child classes. Child
classes can directly use the parent class code.
• Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the
ways by which Java achieves Run Time Polymorphism.
• Abstraction: The concept of abstract where we do not have to provide all details, is achieved
through inheritance. Abstraction only shows the functionality to the user.

Use of Inheritance in Java:


• The extends keyword is used for inheritance in Java. Using the extends keyword indicates you are
derived from an existing class.
Syntax :
class ChildClass_Name extends ParentClass_Name
{
//methods and fields
} 10
Example
class Engineering {
String branch = "CSE";
}
class Student extends Engineering{
int age = 22;
}
class MyClass {
public static void main(String args[])
{
Student S1 = new Student();
System.out.println("Age : " + S1.age);
System.out.println("Branch : " + S1.branch);
} } }

11
Types of Inheritance
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance

• On the basis of class, there can be three types of inheritance in java: single, multilevel
and hierarchical.

• In java programming, multiple and hybrid inheritance is supported through interface


only. We will learn about interfaces later.

12
Types of 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.

13
Example
class Engineering {
String branch = "CSE";
public void uniname()
{
System.out.println("My University name is Galgotias ");
} }
class Student extends Engineering{
int age = 22;
public void sname()
{
System.out.println("Student name is Shubham ");
} }
class MyClass1 {
public static void main(String args[])
{
Student S1 = new Student();
S1.sname();
S1.uniname();
System.out.println("Age : " + S1.age + "\nBranch : " + S1.branch);
} }
14
Types of Inheritance
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.

15
Types of Inheritance
class Myclass1 {
public void printName() {
System.out.println("Shubham");
} }
class Myclass2 extends Myclass1 {
public void printAge() {
System.out.println("Age is 22");
} }
class Myclass3 extends Myclass2 {
public void printBranch() {
System.out.println("CSE");
} }
public class MainClass {
public static void main(String[] args) {
// Creating an object of class Three
Myclass3 obj3 = new Myclass3();
obj3.printName();
obj3.printAge();
obj3.printBranch();
} }
16
Types of Inheritance
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.

17
Types of Inheritance
// Main Class
class A {
public class Main {
public void print_A()
public static void main(String[]
{ System.out.println("Class A"); } args)
} {
class B extends A { B obj_B = new B();
public void print_B() obj_B.print_A();
{ System.out.println("Class B"); } obj_B.print_B();
}
class C extends A { C obj_C = new C();
public void print_C() obj_C.print_A();
{ System.out.println("Class C"); } obj_C.print_C();
}
class D extends A {
D obj_D = new D();
obj_D.print_A();
public void print_D()
obj_D.print_D();
{ System.out.println("Class D"); }
} 18
}
Types of Inheritance
4. Multiple Inheritance (Through Interfaces)
• In Multiple inheritances, one class can have more than one superclass 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 Interfaces. In the image below, Class C is derived from
interfaces A and B.

19
Types of Inheritance
5. Hybrid Inheritance
• It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple
inheritances with classes, hybrid inheritance involving multiple inheritance is also not possible
with classes.
• In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve multiple
inheritance to implement Hybrid inheritance.

20
Types of Inheritance
• Advantages Of Inheritance in Java:
1. Code Reusability: Inheritance allows for code reuse and reduces the amount of code that needs to be
written. The subclass can reuse the properties and methods of the superclass, reducing duplication of
code.
2. Abstraction: Inheritance allows for the creation of abstract classes that define a common interface for a
group of related classes. This promotes abstraction and encapsulation, making the code easier to
maintain and extend.
3. Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to model
real-world objects and their relationships.
4. Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to take on
multiple forms. Subclasses can override the methods of the superclass, which allows them to change
their behavior in different ways.
• Disadvantages of Inheritance in Java:
1. Complexity: Inheritance can make the code more complex and harder to understand. This is especially
true if the inheritance hierarchy is deep or if multiple inheritances is used.
2. Tight Coupling: Inheritance creates a tight coupling between the superclass and subclass, making it
difficult to make changes to the superclass without affecting the subclass.
21

You might also like