5. 5
OOP
ο± Class ααΊααΆαααααΆαααΌααααααΆαααααααΎ Object α¬βααΆαα αααααααααααα Object α
ο± Java ααααααΎααααααα·ααααααα Class α αΎαααΈαααααΎ Object α
α αΎα’αααΈαα ααΆ Class ?
ο± Instance Variable ( Data Member ) αα·α Method α α
Instance Variable αα·α Method ααΊααΆ Member αααα Classα
6. 6
OOP
class class-name{
type instance-variable1;
type instance-variable2;
//β¦.
type instance-variableN;
type methodname 1(parameter-list){
//body of method
}
type methodname 2(parameter-list){
//body of method
}
type methodname N(parameter-list){
//body of method
}
ααΆααααααΌαα αααα Class
7. 7
OOP
Class Vehicle{
int passengers; //number of passengers
int fuelcap; //fuel capacity in liters
int kpl; //fuel consumption in kilometers per liter
}
ααΆααααααΎ Class
o Class α’αΆα αααααΎ ααΌαααααααααα·ααααα (Data type) ααααΈαα½α α αα α»αααααΈαααα ααααααα
αα·αααααααααΈα ααααα ααααΆβ Vehicle αα αΎααααΎααααααααααΎααα ααααααα αΎαααΈααααααΆα
Objects α
Vehicle minivan = new Vehicle();
11. 11
Encapsulation
ο± Encapsulation αααΆααααααα½ααα OOP conceptα
ο± ααααΎαα’αΆα Field αα αα α»α class αα½ααα·αα’ααααα α αα’αΆαααΆαααΆα access
α αΌαααΈ class ααααααΆαααΊ variable αα αα α»α class αα½αααα αΌαααΆα hide
ααΈ class αα αα αΎααα’αΆα access ααΆααΆαααα method getter()
αα·αβsetter()α
α αΎα’αααΈαα ααΆ Encapsulation ?
12. 12
Encapsulation
ο± Field αααα class α’αΆα ααΆαα αααΆ Read-only α¬ Write-only.
ο± Class α’αΆα αααααααααααααΆαααααα α»ααα αααΎ data α αααααααΎαααααΆαα codeα
ααααα αα·αααα Encapsulation ?
ο± α’αααααααααΎαααααΆαα class αα·α ααααΈαααααα α class ααα α»α(store) αα·αααααα
ο± Class α’αΆα αα αΌαααΌα data type αα field αα½αα
ο± α’αααααααααΎαααααΆααα class αα·αααα αΌαααΆααα αΌα code ααααααα
13. 13
Encapsulation
public class TestJava {
private int id;
private String name;
private int age;
public void setId(int id){this.id=id;}
public int getID(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
public void setAge(int age){
if(age<0){
this.age=1;
}else{
this.age=age;
}
}
public int getAge(){return age;}
}
Public class DemoEncap{
public static void main(String args[]){
TestJava obj= new TestJava();
obj.setId(100);
obj.setName("Hong");
obj.setAge(-1);
System.out.println("ID:"+
obj.getID() + "nName:"+ obj.getName()+
"nAge:"+ obj.getAge());
}
}
14. 14
Inheritance
α αΎα’αααΈαα ααΆ Inheritance?
ο± Inheritance ααΊααΆααααααα αααΈ Class αα½ααα Class αα½ααααααααα α
ο± Class α αααααα ααααα’αΆααααα ααΆ Parent Class αα αΎα Class α αααα½α ααααα ααΆ Child Classα
ο± Inheritance α’ααααα α α·α²αα Class αα½αααααααΎαααααΆαααα properties αα·α methods αα class αααααααα ααΆα α
ο± Inheritance ααΊααΆ compile-time mechanism α super-class α’αΆα ααΆα subclasses αααα½αα αΆααα½αα
αααΆααα subclass ααΆαααα ααα super-class αα½ααααΆααα αα α
16. 16
Inheritance
class ClassA{
int a=;
public void add(int x, int y){
a = x+y;
System.out.println("The sum of the given numbers:"+a);
}}
public class ClassB extends ClassA{
public void multiplication(int x, int y){
a= x*y;
System.out.println("The product of the given
numbers:"+a);
}
public static void main(String args[]){
int a = 20, b = 10;
ClassB sub = new ClassB ();
sub.add(a, b);
sub.multiplication(a, b);
}
}
ο± α§αα ααα αααΎαααΆα ClassααΈα ααΊ
ClassA αα·αClassB α αClassBααααΎααΆα
Inheritannce αααααΈ ClassAα
17. 17
Inheritance
ο± α§αα ααα
public class Sub_class extends Super_class {
int num = 10; //display method of sub class
public void display(){
System.out.println("This is the display method of subclass");}
public void my_method(){
Sub_class sub = new Sub_class();//Instantiating subclass
sub.display();//Invoking the display() method of sub class
super.display();//Invoking the display() method of superclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
//printing the value of variable num of superclass
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]){
Sub_class obj = new Sub_class();
obj.my_method();
}
class Super_class{
int num = 20; //display method of superclass
public void display(){
System.out.println("This is the display method
of superclass");}}
18. 18
Inheritance
ο± α§αα ααα Public Interface Motor{
void speedUp(int increment);
}
class MyMotor implement Motor{
int speed = 0;
void speedUp(int increment) {
speed = speed + increment;
}
void printStates() {
System.out.println("My motor speed" +speed);
}
}
28. 28
Polymorphism
α αΎα’αααΈαα ααΆ Runtime Polymorphism ?
ο± Method overriding ααΊααΆα§αα ααα αα’ α αΆααααΆαα runtime polymorphism α
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
ο± ααΆαααααΎααα αααΎαα§αα αααα αΎαααΈαααααΆααα αΆαα
public class X {
public void methodA() //Base class method
{ System.out.println
("hello, I'm methodA of class X");
}
}
public class Y extends X {
public void methodA() //Derived Class method
{ System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z {
public static void main (String args []) {
X obj1 = new X(); // Reference and object X
X obj2 = new Y(); // X reference but Y object
obj1.methodA();
obj2.methodA();
}
}
Output:
hello, I'm
methodA of
class X
hello, I'm
methodA of
class Y
29. 29
Polymorphism
α αΎα’αααΈαα ααΆ Compile time Polymorphism ?
ο± Method overloading ααΊααΆα§αα ααα αα’ α αΆααααΆαα compile time polymorphism α
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
ο± ααΆαααααΎααα αααΎαα§αα αααα αΎαααΈαααααΆααα αΆαα
class X {
void methodA(int num) {
System.out.println ("methodA:" + num);
}
void methodA(int num1, int num2)
{
System.out.println ("methodA:" + num1 + "," + num2);
} double methodA(double num) {
System.out.println("methodA:" + num); return num;
}
} class Y {
public static void main (String args []) {
X Obj = new X(); double result; Obj.methodA(20);
Obj.methodA(20, 30); result = Obj.methodA(5.5);
System.out.println("Answer is:" + result);
}
}
Output:
methodA:20
methodA:20,30
methodA:5.5
Answer is:5.5
33. 33
Polymorphism
α αΎα’αααΈααΆβMethod Overriding ?
ο± Method Overriding ααΆαααα αα αΌα αααα βα αΆααα½α αα·α data type αα parameter α αΌα αααα α
public class BaseClass { public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
public class DerivedClass extends BaseClass {
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}
public class TestMethod {
public static void main (String args []) { // BaseClass reference and object
BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object
BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class
obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride();
}
}
Output:
I'm the method of BaseClass I'm the method
of DerivedClass