OOPS Concepts - Java
OOPS Concepts - Java
2017
1
OOPs (Object Oriented Programming System)
• Object
• Class
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
2
Object and Class in JAVA
In this chapter, we will look into the concepts - Classes and Objects.
Object − Objects have states and behaviours.
If we consider a Student ,
its state is - Name, Roll No
its behaviour is - SetName, SetRollNo,
3
Object and Class in JAVA
class <class_name>{
data member;
method;
}
4
Object and Class in JAVA
public class Student {
String name;
int rollno;
void SetName(String nm)
{
name = nm;
}
void SetRollNo(int rno)
{
rollno = rno;
}
void DisplayInfo()
{
System.out.println(name +" is student with Roll number "+rollno);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 =new Student();
Student s2 =new Student();
s1.SetName("John");
s2.SetName("Maria");
s1.SetRollNo(1002);
s2.SetRollNo(1005);
s1.DisplayInfo();
s2.DisplayInfo();
}
}
5
Java Package in JAVA
6
Access modifiers in JAVA
The access modifiers in java specifies accessibility (scope) of a data member,
method, constructor or class.
There are 4 types of java access modifiers:
• private
• default
• protected
• Public
8
Constructor in JAVA
package classobject;
10
Encapsulation in JAVA
•Provide public setter and getter methods to modify and view the variables values.
11
Encapsulation in JAVA
public class EncapTest {
private String name;
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
12
Inheritance in Java
13
Inheritance in Java
14
Inheritance in Java
The extends keyword indicates that you are making a new class that derives
from an existing class. In the terminology of Java, a class that is inherited is
called a super class. The new class is called a subclass.
15
Inheritance in Java
package Inheritance;
class Animal {
public void heart() {
System.out.println("We have heart");
}}
class Mammal extends Animal {
public void MammaryGlands() {
System.out.println("We have Mammary glands!");
}}
class Reptile extends Animal {
public void Hiss() {
System.out.println("Hiss Hiss!!");
}}
public class dog extends Mammal {
public void bark() {
System.out.println("Dogs bark Bow Bow!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
dog d = new dog();
d.bark();
d.MammaryGlands();
d.heart();
}}
16
super keyword in Java
package classobject;
class Bike4 {
int speed=50;
}
class superkwrd extends Bike4{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
superkwrd b=new superkwrd();
b.display();
}
}
17
super keyword in Java
• The super keyword in java is a reference variable that is used to refer
immediate parent class object.
18
Final keyword in Java
The final keyword in java is used to restrict the user. The java final keyword
can be used in many context.
Final can be:
• variable
• method
• Class
1) Java final variable
If you make any variable as final, you cannot change the value of final
variable(It will be constant).
19
this keyword in Java
In java, ‘this’ is a reference variable that refers to the current object.
20
this keyword in Java
public class thiskwrd {
int id;
String name;
thiskwrd(int id,String name){
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
thiskwrd s1 = new thiskwrd(111,"Karan");
thiskwrd s2 = new thiskwrd(222,"Aryan");
s1.display();
s2.display();
}
}
21
Polymorphism in JAVA?
There are two types of polymorphism in java: compile time polymorphism and
runtime polymorphism. We can perform polymorphism in java by method
overloading and method overriding.
22
Method Overloading in JAVA?
23
Method Overloading in JAVA?
In this example, we have created two overloaded methods, first sum method
performs addition of two numbers and second sum method performs
addition of three numbers.
class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
24
Method Overloading in JAVA?
In this example, we have created two overloaded methods that differs in data type.
The first sum method receives two integer arguments and second sum method receives
two double arguments.
class Calculation2{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
25
Method Overriding in JAVA?
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
26
Method Overriding in JAVA?
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
27
Method Overriding in JAVA?
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
As per dictionary, abstraction is the quality of dealing with ideas rather than
events. Likewise in Object-oriented programming, abstraction is a process of
hiding the implementation details from the user, only the functionality will
be provided to the user.
29
Abstraction in JAVA?
Abstract Class
A class which contains the abstract keyword in its declaration is known as
abstract class.
Abstract classes may or may not contain abstract methods, i.e., methods
without body ( public void get(); )
But, if a class has at least one abstract method, then the class must be
declared abstract.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
In this example, Bike the abstract class that contains only one abstract
method run. It implementation is provided by the Honda class.
31
HELPING HANDS
Thank You
COPYRIGHT NOTICE
Copyright © 2017 Helping Hands
All rights reserved. These materials are confidential and proprietary to Helping hands and no part of these materials should be reproduced, published in any form by any
means, electronic or mechanical including photocopy or any information storage or retrieval system nor should the materials be disclosed to third parties without the
express written authorization of Helping Hands. 32