Beruflich Dokumente
Kultur Dokumente
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 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.
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%)
// declare methods
}
Continue...
Internal addition by the compiler
Relationship between classes and interfaces
Example
interface Printable{
void print();
}
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”);
❖ String s = null;
System.out.println(s.length()); // NullPointerException
❖ String s = “abc”;
int i = Integer.parseInt(s); // NumberFormatException