Professional Documents
Culture Documents
Java Programming Chapter Two
Java Programming Chapter Two
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
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
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
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.
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?
} }
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.
Class Test1{
final class
◦ If you make any class as final, you cannot extend it.
End of Chapter