Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Java Programming

Chapter Two
Agenda
Access Modifiers (public, protected, private)
Packages
Inheritance
How to achieve multi-inheritance in java?
Java polymorphism
Method overloading
Method overriding
Super keyword
final keyword
Access Modifiers
Access Modifiers are keywords in java language, that set the accessibility
or scope of classes, methods, attributes and other members.
Java Language uses four access modifiers:
1. Public
2. Private
3. Protected
4. Package (Default)
Access Modifiers
Access Modifiers For classes:
you can use either public or default.

Modifier Description
public The class is accessible by any other class

default The class is only accessible by classes in the same package.


This is used when you don't specify a modifier.
Access Modifiers
For attributes and methods you can use the one of the following:

Modifier Description
Public The code is accessible for all classes
Private The code is only accessible within the declared class
Default The code is only accessible in the same package. This is
used when you don't specify a modifier.
Protected The code is accessible in the same package
and subclasses.
Access Modifiers
Access Modifiers
Access Within Within Same Outside Global
Modifiers class package package package
and and
subclass subclass
Public Yes Yes Yes Yes Yes
Protected Yes Yes Yes Yes No
Default Yes Yes Yes No No
Private Yes No No No No
Java Packages
A package in Java is used to group related classes, subclasses and interfaces.
Think of it as a folder in a file directory.
The Java platform provides an enormous class library (a set of packages) suitable
for use in your own applications. This library is known as the "Application
Programming Interface".
Packages are divided into two categories:
1. Built-in Packages (packages from the Java API)
2. User-defined Packages (create your own packages)
Built-in Packages
The Java API is a library of prewritten classes, that are free to use.
The library contains components for managing input, database programming, and
much more.
The library is divided into packages and classes. Meaning you can either import a
single class, or a whole package.
To use a class or a package from the library, you need to use the import keyword:
Syntax:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
Built-in Packages
Import a Class

If you find a class you want to use, for example, the Scanner class, which is used to get
user input, write the following code:
Example
import java.util.Scanner;
In the example above, java.util is a package, while Scanner is a class of
the java.util package.
To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation.
In our example, we will use the nextLine() method, which is used to read a complete
line.
Example

Using the Scanner class to get user input:

import java.util.Scanner;
class MyClass {
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}}
User-defined Packages

To create a package, use the package keyword:


Syntax:
package package-name;

Example:
package mypack;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
}}
Inheritance
Inheritance can be defined as the process where one class acquires the
properties (methods and variables) of another class.
With the help of inheritance the information is made manageable in a
hierarchical order.
The class which inherits the properties of other class is known as
subclass (derived class. Child class).
And the class whose properties are inherited is known as
superclass(base class, parent class).
Extends keyword
extends is the keyword used to inherit the properties of a class.
Syntax:
Class Super {
……………
}
Class SubClass extends Super {
………………….
}
Example
class Employee{
int sal=4; }
public class Programmer extends Employee{
int b=2;
public static void main(String[] args) {
Programmer obj = new Programmer();
System.out.println(obj.sal);
System.out.println(obj.b); }}
Types of inheritance
There are mainly four types of inheritance in java:
1. Single level inheritance
 When one class acquires the properties of another one class.

2. Multi level inheritance


 When one class inherit from a derived class, there by making this derived class the base
class for the new class.

3. Hierarchical inheritance
 When one class is inherited by many subclasses

4. Hybrid inheritance
 It can be the combination of single, multi level and hierachal inheritance
Types of inheritance
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A
and B classes.
If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-
time error if you inherit 2 classes.
We can achieve Multiple inheritance in java using Interface.
class A{  
Multiple inheritance?
void msg() {

System.out.println("Hello");}   }  

class B {  

void msg() {

System.out.println("Welcome");}   }  

class C extends A,B { //suppose if it were

 }

 Public Static void main(String args[]){  

   C obj=new C();  

   obj.msg(); //Now which msg() method would be invoked?  

}  }

Compile Time Error


Advantages
We can create new classes that are built upon existing classes.
For Method Overriding (so runtime polymorphism can be
achieved).
For Code Reusability :
When we inherit from an existing class, can reuse methods and fields of
parent class, and can add new methods and fields also.
Polymorphism
Polymorphism means "many forms", and it occurs when we have
many classes that are related to each other by inheritance.

Polymorphism in java is a concept by which we can perform a


single action by different ways.

Polymorphism can perform in java by method overloading and


method overriding.
Polymorphism

There are two types of polymorphism in java


1) Compile time polymorphism (static)
◦ Resolved at compile time.

2) Runtime polymorphism(dynamic)
◦ Resolved at run time
Method Overloading
If a class have multiple methods by same name but different
parameters, it is known as Method Overloading.
Method overloading increases the readability of the program.

There are two ways to access overload the method in java


1. By changing number of arguments
2. By changing the data type
Method Overloading
public class A
{
static int plusMethodInt(int x, int y) {
return x + y;
}
static double plusMethodInt(double x, double y) {
return x + y;
}
public static void main(String[] args)
{
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodInt(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
} }
Method Overriding
If subclass has the same method as declared in the parent class, it is
known as method overriding in java.
The methods must have the same name and same parameter as in
the parent class.
Method overriding is used to provide specific implementation of a
method that is already provided by its super class.
Method overriding is used for runtime polymorphism.
Method Overriding

Rules for Java Method Overriding


method must have same name as in the parent class

method must have same parameter as in the parent class.

A method declared final cannot be overridden.


Example of method overriding

Consider a scenario where Bank is a class that provides


functionality to get the rate of interest.

However, the rate of interest varies according to Banks. For


example, B1, B2 and B3 banks could provide 8%, 7% and 9% rate of
interest.
Example of method overriding
Class Bank{

Int getRate() {return 0;}}

Class B1 extends Bank{

Int getRate(){ return 8; }}

Class B2 extends Bank{

Int getRate(){ return 7; }}

Class B3 extends Bank{


int getRate(){ return 9; }}

Class Test1{

Public static void main(String[] args){

B1 obj1= new B1();

B2 obj2 = new B2();

B3 obj3= new B3();

System.out.println(“B1 rate :”+obj1.getRate());

System.out.println(“B2 rate :”+obj2.getRate());

System.out.println(“B3 rate :”+obj3.getRate()); } }


Super keyword
The super keyword refers to superclass (parent) objects.
It is used to call superclass methods, and to access the superclass
constructor.
The most common use of the super keyword is to eliminate the
confusion between super classes and subclasses that have methods
with the same name.
“super” keyword is used to call overridden methods at compile
time.
“final” keyword
The java final keyword can be used as :
final variable
◦ If you make any variable as final, you cannot change the value of final
variable(It will be constant). Final variable can only be initialized once.
final method
◦ If you make any method as final, you cannot override it.

final class
◦ If you make any class as final, you cannot extend it.
End of Chapter

You might also like