Object Oriented Programming (OOPS)
object issue: code repettition with individual objects
Class & Object (Blueprint)
HTML:
let john {
firstname: 'John ',
lastname: 'Doe' ,
dob: 1992,
phoneNo: 1234567890, //object
isEmp10yed: true,
getDetails: function ( ) { //function
const age = new Date( ).getFuIIYear( )- this.dob;
console. log(
'${this.firstname} ${this.lastname} is ${age} old & contact no is ${this.phoneNo}' ); //calculzte to age year -age
};
getEmp1oyementStatus: function ( ) {
console. log(
'$(this.firstname) is ${this.isEmp1oyed ?'employed': 'unemployed'});
};
console.log(john); // obj block call aagum
john. getDetai1s() ; // getdetails functtion call agum
john. getEmp1oyementStatus( ) ; // getemployement function call aagum
class:
//class name
class Person {
// construor inputs
constructor(firstname, lastname, dob,phoneNo, isEmpoyrd) {
var n
this. firstname = firstname;
this. lastname = lastname; // value assign
this. dob = dob;
this. phoneNo = phoneNo; /// blue print
this. isEmp1oyed = isEmp10yed;
}
getDetails( ) { //function
const age = new Date( ).getFuIIYear( )- this.dob;
console. log(
'${this.firstname} ${this.lastname} is ${age} years old & contact no is ${this.phoneNo}' ); //calculzte to age year -age
},
getEmp1oyementStatus( ) {
console. log(
'${this.firstname}'${this.firstname} is ${this.isEmp1oyed ?'employed': 'unemployed'});
};
//input kudukkanum
let anni = new Person( 'Star' , ' Light ', 1997,123456789,true) ; // obj creation or instant creation
anni. getDetai1s(); // f calling
anni. getEmp1oyementStatus ( ) ;
// var name
let homelander = new Person( ' Anatony'.' star ', 1997,123456789,false) ;
homelander.getDetai1s();
homelander.getEmp1oyementStatus ( ) ;
Inheritance (Parent Child)
//parent
//class name
class Person {
construor inputs
constructor(firstname, lastname, dob,phoneNo, isEmpoyrd) {
//var n
this. firstname = firstname;
this. lastname = lastname; // value assign
this. dob = dob;
this. phoneNo = phoneNo; /// blue print
.
this. isEmp1oyed = isEmp1oyed;
}
getDetails( ) { //function
const age = new Date( ).getFuIIYear( )- this.dob;
console. log(
'${this.firstname} ${this.lastname} is ${age} years old & contact no is ${this.phoneNo}' ); //calculzte to age year -age
},
getEmp1oyementStatus( ) {
console. log(
'${this.firstname}'${this.firstname} is ${this.isEmp1oyed ?'employed': 'unemployed'});
};
//child person ulladha empoyee inherit panna poooguthu
//cls name // inherit function
Class Employee extends person{ // ippo parent ullla ellathiyum emp;oyee access pannum
// child //parent
constructor(
firstname,
lastname,
dob ,
phoneNo,
isErnp10yed,
jobTit1e,
company );
{
// employee ullatha parentku anupaa
super(firstname, lastname, dob,phoneNo,isEmployed) ; // functio ullaa pogum
// input
//input
this.jobTit1e = jobTit1e;
this. company = company;
getJobDetaiIs() {
console. log(
'${this.firstname)${this. lastname} work as a ${this.jobTit1e) at ${this. company}');
}
}
let employeel = new Employee{
'Arun', //object creation
'Kumar '
1990,
9876543210,
true,
"softwar developer",
"tech corp"};
employeel. getJobDetai1s ( ) ;
employeel. getE1oyementStatus ( ) ; // parent ulla irukka estatus cal pannum
employeel. getDétai1s ( ) ;
//console.log( employeel.firstname); // paticular vale access panna
//console.log( employeel.lasttname);
let employee2 = new Employee{
'Ranjith ',
'Kumar '
1990,
9876543210,
true,
"softwar developer",
"tech corp"};
employee2. getJobDetai1s ( ) ;
employee2. getE1oyementStatus ( ) ;
employee2. getDétai1s ( ) ;
Polymorphism (Same method in different form)
//parent
//class name
class Person {
construor inputs
constructor(firstname, lastname, dob,phoneNo, isEmpoyrd) {
//var n
this. firstname = firstname;
this. lastname = lastname; // value assign
this. dob = dob;
this. phoneNo = phoneNo; /// blue print
.
this. isEmp10yed = isEmp10yed;
}
getDetails( ) { //function
const age = new Date( ).getFuIIYear( )- this.dob;
console. log(
'${this.firstname} ${this.lastname} is ${age} years old & contact no is ${this.phoneNo}' ); //calculzte to age year -age
};
getEmp10yementStatus( ) {
console. log(
'${this.firstname}'${this.firstname} is ${this.isEmp1oyed ?'employed': 'unemployed'});
},
};
//child person ulladha empoyee inherit panna poooguthu
//cls name // inherit function
Class Employee extends person{ // ippo parent ullla ellathiyum emp;oyee access pannum
// child //parent
constructor(
firstname,
lastname,
dob ,
phoneNo,
isErnp10yed,
jobTit1e,
company }
{
// employee ullatha parentku anupaa
super(firstname, lastname, dob,phoneNo,isEmployed) ; // functio ullaa pogum
// input
//input
this.jobTit1e = jobTit1e;
this. company = company;
// Overriding the getdetails method (parent ulla irukkuratha override pannum its called polymorphism)
getDetai1s() {
const age = new Date ().gettFullYear()- this.dob;
console. log (
${this.firstname} ${this.lastname} is ${age} years old,work as a ${this.jobTit1e} at ${this.conpany});
}
getJobDetaiIs() {
console. log(
'${this.firstname)${this. lastname} work as a ${this.jobTit1e) at ${this. company}');
}
}
let employeel = new Employee{
'Arun', //object creation
Kumar '
1990,
9876543210,
true,
"softwar developer",
"tech corp"};
employeel. getDétai1s ( ) ;
let employee2 = new Employee{
'Ranjith ',
Kumar '
1990,
9876543210,
true,
"softwar developer",
"tech corp"};
employee2. getDétai1s ( ) ;
class Student extends Person {
constructor(firstnne, lastname, dob, phoneNo,school, grade),{
super(firstnæ, lastnæ, dob, phonek), isEmployed);
this. school school;
this.grade = grade;
}
// Overriding the getDetails method
getDetai1s() {
const age = new Date().getFu11Year()- this .dob;
console. log(
'${this.firstname)${this. lastname} is ${age) grade ${this. grade} is ${this. school} ');
}
}
let student = new Student(
' Lakshmi ',
' Priya',
1995,
8765432109,
true,
'Sunrise School',
"10th"
);
student. getDetai1s() ;
Encapsulation (protected )
//class name
class BankAccount {
//input
#balance; //private field //bank account vech access Panna mudiyaathu
constructor (initialBalance) {
// ini balance set to private
this.#balance = initialBalance;
}
// Fn //Input
deposit(amount) {
if (amount > 0) {
this.#balance += amount;
console.log(Deposited ${amount). New Balance $(this.#balance}) ;
} else {
console.log(Invalid deposit amount);
}
}
withdraw (amount) {
if (amount > 0 && amount <= this.#balance) {
this.#balance -= amount;
console.log("Withdraw ${amount). New Balance ${this.#balance));
} else {
console.log(Insufficient funds or invalid amount.");
}
}
//balance paakka indha part
getBalance() {
return this.#balance;
}
}
let savningAccount = new BankAccount(1000);
savningAccount deposit (500);
savningAccount withdraw(200);
savningAccount withdraw(30000);
savningAccount deposit (-1000);
//saving Account. #balence 100000; Error ' private field
console.log(savningAccount.getBalance()); // balance paaka
Output:
Deposited 500. New Balance 154
Withdraw 200. New Balance 1200
Insufficient funds Invalid amount.
Invalid deposit amount
Top comments (0)