Content text OOP - 2022W Paper Solution(1).pdf
In this example, the Animal class is the parent class, and the Dog class is the child class that extends the Animal class. The Dog class overrides the makeSound method of the Animal class to provide its own implementation. When the makeSound method is called on the animal object, the output is "The animal makes a sound". When the makeSound method is called on the dog object, the output is "The dog barks". Question 2 (a) Explain constructor with the help of an example. public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; System.out.println("A new person has been created!"); } } In this example, we define a class called "Person" with two private attributes: "name" and "age". We also define a constructor for the class that takes two arguments, “name” and “age“. Inside the constructor, we use the "this" keyword to refer to the current instance of the class and assign the values of the "name" and "age" parameters to the corresponding attributes of the instance. We also print a message to indicate that a new person has been created. When a new instance of the Person class is created using the constructor, the “name” and “age” values are set and the message is printed to the console. For example: Person john = new Person("John", 25); This code creates a new instance of the Person class with the name “John” and age “25”, and the message “A new person has been created!” is printed to the console. (b) List out different methods available for String class in java and explain any two with proper example. The String class in Java has a large number of methods available for manipulating and analyzing strings. Here are some of the most commonly used methods: length(): This method returns the length of the string, which is the number of characters in the string.
charAt(int index): This method returns the character at the specified index in the string. The index is zero-based, meaning that the first character in the string has an index of 0. concat(String str): This method concatenates the specified string to the end of the original string and returns a new string that represents the concatenation. substring(int beginIndex): This method returns a substring of the original string, starting at the specified index and continuing to the end of the string. indexOf(char c): This method returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found. replace(char oldChar, char newChar): This method replaces all occurrences of the specified old character with the specified new character and returns a new string that represents the modified string. String str = "hello"; int len = str.length(); // len is 5 String str = "hello"; char c = str.charAt(1); // c is 'e' (c) Explain all access modifiers and their visibility as class members. In object-oriented programming, access modifiers are keywords used to set the accessibility or visibility of class members (fields, methods, and inner classes) in a class. There are four access modifiers in Java: Public: Public members are accessible to all classes, regardless of the package they belong to. Public members can be accessed by any other class in the same program. Private: Private members are only accessible within the same class. They cannot be accessed by any other class, even if it belongs to the same package. Protected: Protected members are accessible within the same class, as well as any subclass that extends the class. They cannot be accessed by any other class, including those in the same package. Default (also known as package-private): Members with no access modifier specified are only accessible within the same package. They cannot be accessed by any other class outside the package.