CP4 Midterms Lesson 1 PDF
Document Details
![GoodlyThermodynamics](https://quizgecko.com/images/avatars/avatar-15.webp)
Uploaded by GoodlyThermodynamics
Holy Trinity Academy
Tags
Summary
This document appears to be lecture notes or study materials on computer programming, specifically focusing on object-oriented programming concepts in Java, including inheritance and other OOP principles.
Full Transcript
COMPUTER PROGRAMMING 4 MATCH N MATCH OOPS IN JAVA: ENCAPSULATION, INHERITANCE, POLYMORPHISM, ABSTRACTION Object-Oriented Programming System(OOPs) OBJECT-ORIENTED PROGRAMMING SYSTEM(OOPS) -is a programming paradigm based on the concept of “objects” that contain data and methods. W...
COMPUTER PROGRAMMING 4 MATCH N MATCH OOPS IN JAVA: ENCAPSULATION, INHERITANCE, POLYMORPHISM, ABSTRACTION Object-Oriented Programming System(OOPs) OBJECT-ORIENTED PROGRAMMING SYSTEM(OOPS) -is a programming paradigm based on the concept of “objects” that contain data and methods. WHAT IS AN OBJECT? Object: is a bundle of data and its behaviour(often known as methods). OBJECTS HAVE TWO CHARACTERISTICS: THEY HAVE STATES AND BEHAVIORS. Examples of states and behaviors WHAT ARE CLASSES AND OBJECTS IN JAVA? Classes in Java are "local entities" that do not require any memory space without the object. It is a blueprint of Java programming. The class keyword is used to declare a class such as objects, methods, variables, etc. that are situated inside the class. class { // data attributes; // member functions() } class { A class consists of some elements that are // data attributes; Class: It defines the particular class // member functions() Access Modifiers: It defines if the class } should be "public", "private", "package- level (default)" or "protected". Class Name: Give a name to the class Superclass: It mentions the name of the superclass if there are any Interface: Mentions the name of the super interface if there is any Body: It represents the body of the class with {} these curly braces. MyClass is a custom class (defined later in the code). TVL is the variable that will reference an object of type MyClass. new MyClass() is the syntax for creating a new instance (object) of MyClass and assigning it to the variable TVL. The object TVL has two public attributes: num (of type int) and string (of type String), as defined in the MyClass class. You can directly access and modify the values of these attributes because they are public. TVL.num = 41; sets the num attribute of the TVL object to 41. TVL.string = "Welcome Back, TVL!"; sets the string attribute of the TVL object to "Welcome Back, TVL!". System.out.println is used to print the values of num and string to the console. The first println prints the value of num, which is 41. The second println prints the value of string, which is "Welcome Back, TVL!". This defines the MyClass class. num is an integer attribute, and string is a string attribute. Both are public, meaning they can be accessed directly from outside the class (such as in the Main class). THE OOP CONCEPTS IN JAVA DEPEND ON FOUR PILLARS, WHICH ARE: Abstraction Inheritance Encapsulation Polymorphism INHERITANCE In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class- based inheritance), retaining similar implementation. Also defined as deriving new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes. INHERITANCE Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces. To inherit from a class, use the extends keyword. JAVA INHERITANCE Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces. This line 3 creates an instance of the Dog class. The variable sample is a reference to this Dog object. Since Dog is a subclass of Animal, it inherits all the attributes and methods of Animal, including the name attribute and eat() method. Line 4, the name attribute of the sample object (which is an instance of Dog) is set to "Chloe". This attribute is inherited from the Animal class, which is the superclass of Dog. Line 5 calls the display() method of the Dog class. This method is specific to the Dog class and prints the name of the dog to the console. Line 6 calls the eat() method of the Animal class. Since Dog extends Animal, the Dog object inherits the eat() method and can invoke it. Line 11 - The name attribute is a String, and it is inherited by any subclass of Animal (including Dog). Line 12 - The eat() method prints "I can eat!" to the console. This method can be called by any object of Animal or its subclasses, such as Dog. Line 17 - The Dog class extends the Animal class, which means Dog inherits all the fields and methods from Animal. This includes the name attribute and the eat() method. Line 18 - The display() method is specific to the Dog class. It prints the name of the dog by accessing the inherited name attribute from the Animal class. Line 19 - System.out.println("Dog's name is " + name); uses the name attribute (inherited from Animal) and prints "Dog's name is Chloe" to the console when display() is called on a Dog object. Flow of Execution: 1. When the program runs, it creates a Dog object (sample). 2. The name attribute of the sample object is set to "Chloe". 3. The display() method of Dog is called, which prints the dog's name, Chloe, to the console. 4. The eat() method of Animal (which is inherited by Dog) is called, printing "I can eat!". PYTHON INHERITANCE Being an object-oriented language, Python supports class inheritance. It allows us to create a new class from an existing one. The newly created class is known as the subclass (child or derived class). The existing class from which the child class inherits is known as the superclass (parent or base class). This line 2 creates an instance of the Dog class and assigns it to the variable sample. Dog is a subclass that inherits from Animal, so sample will also have the properties and methods of Animal. The name attribute is set to "Chloe". The name attribute is defined in the Animal class (through the __init__ method), so it is available in the Dog class (since Dog inherits from Animal). This calls the display() method, which is defined in the Dog class. The display() method prints the dog's name to the console. This calls the eat() method, which is inherited from the Animal class. The eat() method prints "I can EAT!" to the console. Constructor (__init__): The __init__ method is the constructor for the Animal class. It initializes the object by setting the name attribute to None when an Animal object is created. In the case of the Dog class (which inherits from Animal), this constructor is automatically called when you create a Dog object (because Dog inherits from Animal). eat() Method: The eat() method prints "I can EAT!". This method is available to all objects of the Animal class and its subclasses, like Dog. So when sample.eat() is called, it calls this method from the Animal class. Inheritance: The Dog class inherits from Animal using the syntax class Dog(Animal). This means that all instances of Dog will also have the name attribute and the eat() method from Animal. display() Method: The display() method is specific to the Dog class. It prints "Dog's name is " followed by the name attribute. Since the name attribute is inherited from Animal, it will access the value of name that was set earlier (i.e., "Chloe"). This is a common Python idiom used to ensure that certain code only runs when the script is executed directly (and not when it is imported as a module). __name__ is a special Python variable. If the script is being run directly, __name__ will be equal to "__main__". If the script is imported as a module, __name__ will be set to the name of the script. If the script is executed directly, the main() function is called. FLOW OF EXECUTION: Creating the Dog object: The program starts by calling main(). Inside main(), a new Dog object (sample) is created. When Dog() is instantiated, the __init__() method of Animal is called (since Dog inherits from Animal), initializing the name attribute to None. Setting the name: sample.name = "Chloe" The name attribute of the sample object is set to "Chloe". Calling display(): sample.display() The display() method in the Dog class is called. It accesses the name attribute (which is now "Chloe") and prints: Dog's name is Chloe Calling eat(): sample.eat() The eat() method from the Animal class is called (since Dog inherits from Animal). It prints: I can EAT! In this example, the Car class extends the Vehicle class, inheriting its move() method. The Car class also defines its own move() method and an additional honk() method. When move() is called on a Car object, the overridden method in Car is executed, demonstrating method overriding in inheritance. PAIR UP! Activity 1 - Look for a partner and create a Java and Python Inheritance. 1 SuperClass and 5 Subclasses (Any Topic) 5MP (5 MINUTES PROGRAM) MY TWO CENTS Share what have you learned today! CHECKPOINT #1 1. In Java, what keyword is used to inherit from a parent class? 2. In Python, how do you define a child class that inherits from a parent class? 3. Can Python support multiple inheritance (inheriting from more than one class)? 4. Which keyword in both Java and Python is used to call a method from a parent class? 5. What does if __name__ == "__main__": main() means? ASSIGNMENT - Search for Java and Python Polymorphism