Upload
avice-bond
View
216
Download
0
Embed Size (px)
DESCRIPTION
Object-Oriented Design(OOD) Most recent software design pattern. Objects: real life objects. Objects have two parts: Attributes: properties of the object.E.g. Name, address Methods: functions(things) an object can do.E.g. prepareFood(), buildHouse().....
Citation preview
Software development
For large and complex software use divide and conquer rule.
Software design methods:Structured designObject-Oriented design
Object-Oriented Design(OOD)Most recent software design pattern.
Objects: real life objects.
Objects have two parts:Attributes: properties of the object.E.g. Name,
address......Methods: functions(things) an object can do.E.g.
prepareFood(), buildHouse().....
ClassBlueprint to create Objects.
What an Object represents is shown by its class.
A class describes what all Objects of a particular type have in common.
A class is a must to create Object(s).
An example of a class & its ObjectsClass – Dog
Objects – Scooby, Snowy, Puppy
Attributes – name, color, size, breed....
Methods – run, bark, bite, sit, sleep....
Dog class diagram
The Dog class is a template or blueprint for creating dog objects
Dog classFrom Dog class we can create individual Dog Objects.
Puppy object - an instance(example) of Dog class.
State of an ObjectValues the attributes of an Object are set to.
Could be different for different Objects of the same class.
We get Objects to do things by calling their methods.
Object Orientation AdvantagesRe-usability: once we have defined a class, we can
use it over and over again to create many different objects of that type.Someone else can also use this class to create objects
so we can create class library.
when defining new classes we can compose classes from other existing classes to create complex objects.
Object Orientation AdvantagesEncapsulation - we can create objects from a
class and use its behaviours without needing to know the internal details of how it works.
Access ModifierPublic
Private
Default
Protected
Types of MethodsMethod that does not return valueMethod without parameterMethod with parameter
Method that returns valueMethod without parameterMethod with parameter
Accessing private fields of a classpublic class Dog{
private String name;private int size;private String color;
}
public class Dog{private String name;private int size;private String color;
public Dog(String dName, int dSize, String dColor){
name = dName;size = dSize;color = dColor;
}}
public class Test{public static void main(......){
Dog dObject = new
Dog(“Scooby”,5,”Brown”);}
}
Set methodspublic class Dog{
private String name;private int size;private String color;public void setName(String dName){name = dName;}public void setSize(int dSize){size = dSize;}// do for color..........
}
public class Test{ public static void main(......){
Dog dObject = new Dog();dObject.setName(“Scooby”);dObject.setSize(5);// do for color......
}}
Get methods
public class Dog{//fields//set methodspublic String getName(){
return name;}public int getSize(){
return size;}// do for color........
}
public class Test{ public static void main(......){
Dog dObject = new Dog();System.out.println(“Name: ”+dObject.getName());// do for size.....// do for color......
}}
Scope of a variableThe area/part of the program over which a variable can be referenced (i.e. visible).public class Dog{
private String name;…..........public Dog(){
name=”Scooby”;}
}
This variable is visible and can be referenced from anywhere in the class
Scope of a variable (Contd..)You cannot refer to a variable before its
declaration.Inside a class a variable can be declared at
several different placesIn a class body as class fields =>classlevel
variablesAs parameter of method or constructorIn a method's body or a constructor's bodyWithin a statement block (while or for)
Scope of a variable (Contd..)Class-level
public class Calculate{private int length;private int breadth;private int area;…........................public int area(){length = 6;breadth = 2;area=length * breadthreturn area;}}
Scope of a variable (Contd..)As parameter of method or constructor
public class Calculate{public int area(int length, int breadth){return length*breadth;}}
visible only insidethis method
Scope of a variable (Contd..)In a method's body or a constructor's body
public class Calculate{public int area(int length, int breadth){int len = length;int bre = breadth;return len*bre;}}
Method overloading class MathDemo { public static void areaDemo(double length, double breadth) { double area = length * breadth; System.out.println(“Area of rectangle : ” + area); } public static void areaDemo(double length) { double area = length * length; System.out.println(“Area of square : ” + area); } public static void main(String args[]) { areaDemo(12,15); areaDemo(55); } }
Constructor Overloading class BoxDemo { double volume; public BoxDemo(double length, double breadth, double height) { volume = length * breadth * height; } public BoxDemo(double length) { volume = length * length * length ; } public void volumeDemo() { Systme.out.println(“Volume of box is : “ + volume); }
public static void main(String args[])
{ BoxDemo box1 = new
BoxDemo(5,6,7); box1.volumeDemo();
BoxDemo box2 = new BoxDemo (5);
box2.volumeDemo();}
}