0% found this document useful (0 votes)
18 views25 pages

Week 011 B

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

Week 011 B

Object Oriented Programming REVISION
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Software Design & Analysis

Abdul-Rahman Mahmood
Assistant Professor, Computer Science, FAST-NU

[email protected] reddit.com/user/alphapeeler
alphapeeler.sf.net/pubkeys/pkey.htm www.flickr.com/alphapeeler
pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com
bqb-tsid-asp [email protected]
alphapeeler [email protected]
alphapeeler abdulmahmood-sss
armahmood786 [email protected]
http://alphapeeler.sf.net/ pinterest.com/alphapeeler
Downloads
 https://eclipse.dev/papyrus/

 https://ftp.yz.yamagata-
u.ac.jp/pub/eclipse//modeling/mdt/papyrus/rcp/2024
-06/6.7.0/papyrus-2024-06-6.7.0-win64.zip

 https://www.eclipse.org/downloads/packages/release/
kepler/sr1/eclipse-ide-java-developers (OPTIONAL)
Abstraction
abstract class Animal {
// abstract methods
abstract void move();
abstract void eat();
// concrete method
void label() {
System.out.println("Animal's data:");
}
}

class Bird extends Animal {


void move() {
System.out.println("Moves by
flying.");
}
void eat() {
System.out.println("Eats
birdfood.");
}
}
public class TestBird {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal myBird = new Bird(); Output:
myBird.label(); Animal's data:
myBird.move(); Moves by flying.
myBird.eat(); Eats birdfood.
}
}
public class TestFish {
Output:
public static void main(String[]
Animal's data:
args) {
Moves by swimming.
// TODO Auto-generated method stub
Eats seafood.
Animal myFish = new Fish();
myFish.label();
myFish.move();
myFish.eat();
}
}
public class Test {

public static void main(String[] args) {


// TODO Auto-generated method stub
Animal b = new Bird();
Animal f = new Fish(); Output:
b.label(); Animal's data:
b.move(); Moves by flying.
b.eat(); Eats birdfood.
f.label(); Animal's data:
f.move(); Moves by swimming.
f.eat(); Eats seafood.
}
}
Interfaces
interface Animal { interface Bird {
public void eat(); int numberOfLegs = 2;
public void sound(); String outerCovering = "feather";
} public void fly();
}
public interface Cattle {
int numberOfLegs = 4;
String outerCovering = "hairs";
public void produceMilk();
} class Eagle implements Animal,
Bird {
public class Cow implements public void eat() {
Animal, Cattle{ System.out.println("Eats reptiles
public void eat() { and amphibians.");
System.out.println("Eats grass."); }
} public void sound() {
public void sound() { System.out.println("Has a high-
System.out.println("Has a low- pitched whistling sound.");
pitched mooooo sound."); }
} public void fly() {
public void produceMilk() { System.out.println("Flies up to
System.out.println("Produces up to 10,000 feet.");
4 litres of milk per day."); }
} }
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Eagle myEagle = new Eagle();
myEagle.eat();
myEagle.sound();
myEagle.fly();
System.out.println("Number of legs: " + Output:
Bird.numberOfLegs); Eats reptiles and
System.out.println("Outer covering: " + amphibians.
Bird.outerCovering); Has a high-pitched
System.out.println(); whistling sound.
Cow myCow = new Cow(); Flies up to 10,000 feet.
myCow.eat(); Number of legs: 2
myCow.sound(); Outer covering: feather
myCow.produceMilk();
System.out.println("Number of legs: " + Eats grass.
Cow.numberOfLegs); Has a low-pitched mooooo
System.out.println("Outer covering: " + sound.
Cow.outerCovering); Produces up to 4 litres
} of milk per day.
} Number of legs: 4
Outer covering: hairs
Encapsulation
public class Animal { public double getAverageWeight() {
private String name; return averageWeight;
private double averageWeight; }
private int numberOfLegs; public int getNumberOfLegs() {
public String getName() { return numberOfLegs;
return name; }
} public void setName(String name) {
public void setAverageWeight(double averageWeight) { this.name = name;
this.averageWeight = averageWeight; }
}
public void setNumberOfLegs(int numberOfLegs) {
this.numberOfLegs = numberOfLegs;
}

public static void main(String[] args) {


Animal myAnimal = new Animal();
myAnimal.setName("Eagle");
myAnimal.setAverageWeight(1.5);
myAnimal.setNumberOfLegs(2);
System.out.println("Name: " + myAnimal.getName());
System.out.println("Average weight: " + myAnimal.getAverageWeight()
+ "kg");
System.out.println("Number of legs: " +
myAnimal.getNumberOfLegs()); Output:
}
} Name: Eagle
Average weight: 1.5kg
Number of legs: 2
// Demonstrates more encapsulation as compared to previous example
public class Animal02 {
private String name;
private double averageWeight;
private int numberOfLegs;

public String getName() {


return name;
}
public double getAverageWeight() {
return averageWeight;
}
public int getNumberOfLegs() {
return numberOfLegs; Output:
} Name: Cow
public Animal02() {
this.name = "Cow"; Average weight: 250.34kg
this.averageWeight= 250.34; Number of legs: 4
this.numberOfLegs= 4;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Animal02 myAnimal = new Animal02();
System.out.println("Name: " + myAnimal.getName());
System.out.println("Average weight: " + myAnimal.getAverageWeight()
+ "kg");
System.out.println("Number of legs: " +
myAnimal.getNumberOfLegs());
}
}
//Demonstrates more encapsulation as
compared to previous example
public class Animal03 {
private String name;
private double averageWeight;
private int numberOfLegs;

public void displayDetails() {


System.out.println("Name: " + this.name);
System.out.println("Average weight: " +
this.averageWeight+ "kg");
System.out.println("Number of legs: " + Output:
this.numberOfLegs); Name: Dog
}
public Animal03() { Average weight: 25.34kg
this.name = "Dog"; Number of legs: 4
this.averageWeight= 25.34;
this.numberOfLegs= 4;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Animal03 myAnimal = new Animal03();
myAnimal.displayDetails();
}
}
//Demonstrates more encapsulation as
compared to previous example
public class Animal04 {
private String name;
private double averageWeight;
private int numberOfLegs;

private void displayDetails() {


System.out.println("Name: " + this.name);
System.out.println("Average weight: " +
this.averageWeight+ "kg");
System.out.println("Number of legs: " +
this.numberOfLegs); Output:
} Name: Dog
public Animal04() { Average weight: 25.34kg
this.name = "Dog";
this.averageWeight= 25.34; Number of legs: 4
this.numberOfLegs= 4;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Animal04 myAnimal = new Animal04();
myAnimal.displayDetails();
}
}
public class ElectronicItem {

public static void main(String[] args) {


// TODO Auto-generated method stub
Animal04 myAnimal = new Animal04();
//can't access private member even from same package
//If you change the access specifier protected in Animal, then it can access it.
// see table here : https://www.scaler.com/topics/protected-in-java/
myAnimal.displayDetails();
}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
The method displayDetails() from the type Animal04 is not visible

at w01ex03Encapsulation.ElectronicItem.main(ElectronicItem.java:11)
Inheritance
class Bird {
public String reproduction = "egg";
public String outerCovering = "feather";
public void flyUp() {
System.out.println("Flying up...");
}
public void flyDown() {
System.out.println("Flying down...");
}
}

class Eagle extends Bird {


public String name = "eagle";
public int lifespan = 15;
public void diveDownInWater() {
System.out.println("Dive down to
catch fish...");
}
public void diveUpInWater() {
System.out.println("Dive up to
pick target in air...");
}
}
public class TestEagleInheritance {
public static void main(String[] args) {
// TODO Auto-generated method stub
Eagle myEagle = new Eagle();
System.out.println("Name: " + myEagle.name);
System.out.println("Reproduction: " + myEagle.reproduction);
System.out.println("Outer covering: " + myEagle.outerCovering);
System.out.println("Lifespan: " + myEagle.lifespan);
myEagle.flyUp();
myEagle.flyDown();
myEagle.diveDownInWater();
myEagle.diveUpInWater();
System.out.println();
Bird myBird = new Bird();
System.out.println("Reproduction: " + myBird.reproduction);
System.out.println("Outer covering: " + myBird.outerCovering);
myBird.flyUp();
Output:
myBird.flyDown();
Name: eagle
}
Reproduction: egg
}
Outer covering: feather
Lifespan: 15
Flying up...
Flying down...
Dive down to catch fish...
Dive up to pick target in air...

Reproduction: egg
Outer covering: feather
Flying up...
Flying down...
Polymorphism - Static
(method overloading)
class Bird {
public void fly() {
System.out.println("The bird is flying.");
}
public void fly(int height) {
System.out.println("The bird is flying " + height + " feet high.");
}
public void fly(String name, int height) {
System.out.println("The " + name + " is flying " + height + " feet
high.");
} Output:
} The bird is flying.
The bird is flying 10000 feet high.
The eagle is flying 10000 feet high.
public class TestBirdStatic {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Static polymorphism (method overloading)
Bird myBird = new Bird();
myBird.fly();
myBird.fly(10000);
myBird.fly("eagle", 10000);
}
}
Polymorphism - Dynamic / Runtime
(method overloading)
public class Animal {
public void eat() {
System.out.println("This animal eats insects.");
}
}

class Bird extends Animal {


public void eat() {
System.out.println("This bird eats seeds.");
}
}
public class TestBirdDynamic {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Dynamic polymorphism (method overriding)
Animal myAnimal = new Animal();
myAnimal.eat();
Bird myBird = new Bird(); Output:
myBird.eat(); This animal eats insects.
This bird eats seeds.
}
}

You might also like