0% found this document useful (0 votes)
26 views2 pages

Understanding Object-Oriented Programming

The document discusses object-oriented programming concepts like classes, objects, methods, and relationships between classes. It provides examples of how to define classes for a credit card, customer, product, and shopping cart.

Uploaded by

Aayush Prasad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Understanding Object-Oriented Programming

The document discusses object-oriented programming concepts like classes, objects, methods, and relationships between classes. It provides examples of how to define classes for a credit card, customer, product, and shopping cart.

Uploaded by

Aayush Prasad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

OOP- Based on objects

Useful for analysis,design,coding.


OOP is a methadology to design a program using objects.

Class-is a blueprint from which objects are created.

Method- is a coolection of statements that perform some specific task and return
result to the caller.

Objects- Real world entities that has their own properties and behaviours.

Overview of OOP:
Data hiding is a basic principle of OOP.

Objects are created by a class is an instance


of that class.

Class Relations:
� has-a
� is-a(inheritance)

final keyword is used as a constant so you cant change its value.

======================================

class CreditCard {
private final long cardNumber;
CreditCard(long cardNumber){
[Link] = cardNumber;
}
}

public class Customer {


private final String name;
private CreditCard creditCard;
public Customer(String name,long ccNumber){
[Link]=name;
[Link]= new CreditCard( ccNumber);
}
}

public class Product {


private final String name;
private int price;
public Product(String name,int price){
[Link]= name;
[Link] = price;
}
public int getPrice() {
return price;
}
}
public class ShoppingCart {
private List<Product> products = new ArrayList<>();
public void addProduct(Product product) {
[Link](product);
}
public int getTotalCost() {
return [Link]()
.mapToInt(Product::getPrice)
.sum();
}
}

You might also like