Sie sind auf Seite 1von 33

Java

Outline
❖ Inheritance
❖ Polymorphism
❖ Abstraction
❖ Encapsulation
❖ Exception Handling
❖ Package
Inheritance
❖ It is a mechanism by which one object acquires all the properties and
behaviours of others object.
❖ Inheritance represents the IS-A relationship which is also known as a
parent-child relationship.
❖ Inheritance is used for:
➢ Code reusability
➢ Method Overriding(so runtime polymorphism can be achieved).
❖ Syntax:
➢ class Subclass-name extends Superclass-name{

//-----------//

}
Example
class Employee{
float Salary = 60000;
}

class Programmer extends Employee{


int bonus = 10000;
public static void main(String s[])
{
Programmer pro = new Programmer();
System.out.println(“Salary is: ”+ pro.Salary);
System.out.println(“Bonus is: ” +pro.bonus);
}
}
Types of inheritance in Java
❖ On the basis of class, there can be three types of inheritance in java:
➔ Single
➔ Multilevel
➔ Hierarchical

❖ In java programming, multiple and hybrid inheritance is supported through


interface only.
Example of multilevel
class BabyDog extends Dog
class Animal {
{ void weep()
void eat() {
{ System.out.println(“Weeping”);
System.out.println(“eating”); }
} }
} class Test1
{
class Dog extends Animal public static void main(String s[])
{ {
void bark() BabyDog d = new BabyDog();
{ d.eat();
System.out.println(“Barking”); d.bark();
} d.weep();
} }
}
Example of Hierarchical
class Dog extends Animal
class Animal{
{
void eat()
void bark()
{
{
System.out.println(“eating”);
System.out.println(“barking”);
}
}
}
}
class Cat extends Animal
class Test1
{
{
void meow()
public static void main(String s[])
{
{
System.out.println(“meow”);
Cat c = new Cat();
}
c.eat();
}
c.meow();
}
}
Polymorphism
❖ It is a concept in java by which we can perform “Single action in different
ways”.
❖ There are two types of Polymorphism in java:
➔ Compile-time
➔ Run-time
Method Overloading
❖ If a class has multiple methods having same name but different in
parameters, it is known as Method overloading.
❖ There are two ways to overload the method:
➔ By changing number of arguments
➔ By changing data types of arguments
Method overloading: changing number of
arguments
Example

class Adder{
void sum(int a,int b)
class Test1{
{
public static void main(String s[])
System.out.println((a+b));
{
}
Adder ad =new Adder();
void sum(int a,int b,int c)
ad.sum(1,2);
{
ad.sum(1,2,3);
System.out.println((a+b+c));
}
}
}
}
Method Overloading: changing data type of
arguments
class Adder
{ class Test2
{
void sum(int a,int b)
{ public static void main(String s[])
System.out.println((a+b)); {
} Adder ad =new Adder();
ad.sum(1,2);
void sum(double a,double b) ad.sum(1.5,2.5);
{ }
System.out.println((a+b)); }
}

}
Method Overriding in Java
❖ If a subclass provides the specific implementation of the method that has
been declared by one of its parent class, it is known as method overriding.
❖ Usage of Method Overriding:
➔ Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
➔ Method overriding is used for runtime polymorphism.

❖ Rules for Method Overriding:


➔ The method must have the same name as in the parent class.
➔ The method must have the same parameter as in the parent class.
➔ There must be an IS-A relationship(inheritance)
Example
class AXIS extends Bank{
int getRateOfInterest(){
class Bank{
return 9;
int getRateofInterest(){
}
return 0;
}
}
}
class Test2{
public static void main(String s[]){
class SBI extends Bank{
SBI s = new SBI();
int getRateOfInterest(){
ICICI i = new ICICI();
return 8;
AXIS a = new AXIS();
}
}
System.out.println(“SBI Rate of Interest:
”+ s.getRateOfInterest());
class ICICI extends Bank{
System.out.println(“ICICI Rate of Interest:
int getRateOfInterest(){
”+ i.getRateOfInterest());
return 7;
System.out.println(“AXIS Rate of Interest:
}
”+ a.getRateOfInterest());
}
}
}
Runtime Polymorphism in Java

❖ Runtime polymorphism or Dynamic Method Dispatch is a process in which a


call to an overridden method is resolved at runtime rather than compile-time.

❖ In this process, an overridden method is called through the reference variable


of a superclass. The determination of the method to be called is based on the
object referred by the reference variable.
Continue..
Upcasting
❖ If the reference variable of parent class refers to the object of child class, it is
known as upcasting.
❖ Example
➢ class A{ }
class B extends A
{
A a = new B(); // upcasting
}
Example of Runtime polymorphism

class Bank{
int getRateofInterest(){
return 0;
class Test2{
}}
public static void main(String s[]){
Bank b;
class SBI extends Bank{
b = new SBI();
int getRateOfInterest(){
System.out.println(“SBI Rate of Interest:
return 8;
”+ b.getRateOfInterest());
}}
b= new ICICI();
class ICICI extends Bank{
System.out.println(“ICICI Rate of
int getRateOfInterest(){
Interest: ”+ b.getRateOfInterest());
return 7;
}}
b = new AXIS();
System.out.println(“AXIS Rate of Interest: ”+
class AXIS extends Bank{
b.getRateOfInterest());
int getRateOfInterest(){
}}
return 9;
}}
Abstraction
❖ It is a process of hiding the implementation details and showing only
functionality to the user.
❖ Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the
message. You don’t know the internal processing about the message delivery.
❖ Abstraction lets you focus on what the object does instead of how does it
does.
Continue...
❖ There are two ways to achieve abstraction in java
1. Abstract class(0 to 100%)
2. Interface(100%)

❖ Abstract class in java


➢ A class which is declared as abstract is known as abstract class. It can have abstract and
non-abstract methods. It needs to be extended and its method implemented. It cannot be
instantiated.
Example
abstract class Shape{
abstract void draw();
}

class Rectangle extends Shape{ class Test{


void draw() public static void main(String s[])
{ {
System.out.println(“drawing rectangle”); Shape s = new Rectangle();
}} s.draw();
}
class Circle extends Shape{ }
void draw()
{
System.out.println(“drawing circle”);
}}
Interface
❖ 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 and multiple
inheritance.
❖ Java interface also represents the IS-A relationship.
❖ It cannot be instantiated just like abstract class.
❖ Since Java 8, we can have default and static methods in an interface.
❖ Since Java 9, we can have private methods in interface.
Continue...
Syntax:

interface <interface name>{

// declare methods

// declare methods (which is abstract by default)

}
Continue...
Internal addition by the compiler
Relationship between classes and interfaces
Example
interface Printable{
void print();
}

Test implements Printable{

void print()
{
System.out.println(“Hello”);
}
public static void main(String s[])
{
Test tt = new Test();
tt.print();
}
}
Multiple Inheritance in java
class Test implements Printable,Showable{
public void print(){
System.out.println(“Hello”);
interface Printable{ }
void print();
} print void Show(){
System.out.println(“Welcome”);
interface Showable { }
void show();
} public static void main(String s[])
{
Test tt = new Test();
tt.print();
tt.show();
}}
Encapsulation
❖ Encapsulation in java is a process of wrapping code and data together into a
single unit.
❖ Encapsulation can be achieved by making all the data members of the class
private. Inorder to set and get the data, setter and getter can be used.
Example

class Student{
class Test{
private String name;
Student st = new Student();
public String getName(){
return name;} st.setName(“Alice”);

public void setName(String name) String name = st.getName();


{
this.name = name; System.out.println(“Hello” + name);
} }
}
Exception Handling in Java
❖ The Exception Handling in Java is one of the powerful mechanism to handle
the runtime errors(ClassNotFoundException, IOException, SQLException,
RemoteException) so that normal flow of the application can be maintained.
❖ In Java, an exception is an event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
❖ The java.lang.Throwable class is the root class of Java Exception hierarchy
which is inherited by two subclasses: Exception and Error.
Types of Exception
❖ According to oracle, there are three types of Exception:
➢ Checked Exception
■ Checked Exceptions are checked at compile-time.
■ The classes which directly inherit Throwable class except RuntimeException and Error
are known as checked exception e.g. IOException, SQLException etc.
➢ Unchecked Exception
■ Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
■ The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexoutOfBoundsException etc.
➢ Error
■ Error is irrecoverable e.g. OutOfMemoryError etc.
Common Scenarios of Java Exception
❖ int a = 50/0; // ArithmeticException

❖ String s = null;
System.out.println(s.length()); // NullPointerException

❖ String s = “abc”;
int i = Integer.parseInt(s); // NumberFormatException

❖ int a[] = new int[5];


a[10] = 50; // ArrayIndexOutOfBoundException

Das könnte Ihnen auch gefallen