0% found this document useful (0 votes)
59 views

4 Design Patterns Video Tutorial

The document discusses Java object-oriented programming concepts like classes, objects, inheritance, polymorphism, and static vs non-static methods. It demonstrates creating Dog and Cat subclasses that inherit from an Animal superclass, and shows that subclasses can be referenced by their parent type. Methods like getSound() exhibit polymorphism by having different implementations in subclasses. Static methods can access objects but non-static methods cannot be called from static context.

Uploaded by

Naman Goyal
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)
59 views

4 Design Patterns Video Tutorial

The document discusses Java object-oriented programming concepts like classes, objects, inheritance, polymorphism, and static vs non-static methods. It demonstrates creating Dog and Cat subclasses that inherit from an Animal superclass, and shows that subclasses can be referenced by their parent type. Methods like getSound() exhibit polymorphism by having different implementations in subclasses. Static methods can access objects but non-static methods cannot be called from static context.

Uploaded by

Naman Goyal
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/ 3

1/17/2021 Design Patterns Video Tutorial

001 public class WorkWithAnimals{


002
003 int justANum = 10;
004
005 public static void main(String[] args){
006
007 Dog fido = new Dog();
008
009 fido.setName("Fido");
010 System.out.println(fido.getName());
011
012 fido.digHole();
013
014 fido.setWeight(-1);
015
016 // Everything is pass by value
017 // The original is not effected by changes in methods
018
019 int randNum = 10;
020 fido.changeVar(randNum);
021
022 System.out.println("randNum after method call: " + randNum);
023
024 // Objects are passed by reference to the original object
025 // Changes in methods do effect the object
026
027 changeObjectName(fido);
028
029 System.out.println("Dog name after method call: " + fido.getName());
030
031 System.out.println("Animal Sound: " + fido.getSound());
032
033 // Create a Dog and Cat object with the super class
034 // but the Dog and Cat reference type
035
036 Animal doggy = new Dog();
037 Animal kitty = new Cat();
038
039 System.out.println("Doggy says: " + doggy.getSound());
040 System.out.println("Kitty says: " + kitty.getSound() + "\n");
041
042 // Now you can make arrays of Animals and everything just works
www.newthinktank.com/2012/08/design-patterns-video-tutorial/ 1/3
1/17/2021 Design Patterns Video Tutorial

043
044 Animal[] animals = new Animal[4];
045 animals[0] = doggy;
046 animals[1] = kitty;
047
048 System.out.println("Doggy says: " +animals[0].getSound());
049 System.out.println("Kitty says: " +animals[1].getSound() + "\n");
050
051 // Sends Animal objects for processing in a method
052
053 speakAnimal(doggy);
054
055 // Polymorphism allows you to write methods that don't need to
056 // change if new subclasses are created.
057
058 // You can't reference methods, or fields that aren't in Animal
059 // if you do, you'll have to cast to the required object
060
061 ((Dog) doggy).digHole();
062
063 // You can't use non-static variables or methods in a static function
064
065 // System.out.println(justANum);
066
067 // sayHello();
068
069 // You can't call a private method even if you define it in
070 // the subclass
071
072 // fido.bePrivate();
073
074 // You can execute a private method by using another public
075 // method in the class
076
077 fido.accessPrivate();
078
079 // Creating a Giraffe from an abstract class
080
081 Giraffe giraffe = new Giraffe();
082
083 giraffe.setName("Frank");
084
085 System.out.println(giraffe.getName());
www.newthinktank.com/2012/08/design-patterns-video-tutorial/ 2/3
1/17/2021 Design Patterns Video Tutorial

086
087 }
088
089 // Any methods that are in a class and not tied to an object must
090 // be labeled static. Every object created by this class will
091 // share just one static method
092
093 public static void changeObjectName(Dog fido){
094
095 fido.setName("Marcus");
096
097 }
098
099 // Receives Animal objects and makes them speak
100
101 public static void speakAnimal(Animal randAnimal){
102
103 System.out.println("Animal says: " + randAnimal.getSound());
104
105 }
106
107 // This is a non-static method used to demonstrate that you can't
108 // call a non-static method inside a static method
109
110 public void sayHello(){
111
112 System.out.println("Hello");
113
114 }
115
116 }

www.newthinktank.com/2012/08/design-patterns-video-tutorial/ 3/3

You might also like