Answer The Following Questions, Answers must be correct this is for an Exam. 1.
ID: 3806223 • Letter: A
Question
Answer The Following Questions, Answers must be correct this is for an Exam.
1. List the four main principles of Object Oriented Programming and describe each one in a sentence.
2. Consider a class that contains a private float variable named _secret. Write an Accessor function to get the value of _secret and a Mutator function to change the value of _secret.
3. Specify if the type of class variables (Private, Protected, or Public) that each item can access. Member Functions of a class Friend Functions of a class Functions of a derived class Friend Functions of a derived class Functions of a unrelated class The main function in a program
4. Consider a class the holds a fraction with a numerator and denominator. Provide the declaration for the following operator overloads, do not implement the functions. operator+ for the addition of two fraction objects operator – for the subtraction of one fraction object from another operator* for multiplying two fraction objects operator* for multiplying a fraction object by an integer
5. What does the keyword virtual mean in the context of a class?
Explanation / Answer
Answer1:
The Objects Oriented Programming is constructed over four major principles:
1.Encapsulation,
2.Data Abstraction,
3.Polymorphism
4.Inheritance.
1. Encapsulation:
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition. Typically, only the object’s own methods can directly inspect or manipulate its fields.
Encapsulation is the hiding of data implementation by restricting access to accessors and mutators.
An accessor is a method that is used to ask an object about itself. In OOP, these are usually in the form of properties, which have a get method, which is an accessor method. However, accessor methods are not restricted to properties and can be any public method that gives information about the state of the object.
Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. This type of data protection and implementation protection is called Encapsulation.
A benefit of encapsulation is that it can reduce system complexity.
2. Abstraction
Data abstraction and encapuslation are closely tied together, because a simple definition of data abstraction is the development of classes, objects, types in terms of their interfaces and functionality, instead of their implementation details. Abstraction denotes a model, a view, or some other focused representation for an actual item.
“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch
In short, data abstraction is nothing more than the implementation of an object that contains the same essential properties and actions we can find in the original object we are representing.
3. Inheritance
Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, parent classes or ancestor classes. The resulting classes are known as derived classes, subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.
Subclasses and Superclasses
A subclass is a modular, derivative class that inherits one or more properties from another class (called the superclass). The properties commonly include class data variables, properties, and methods or functions. The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. The software inherited by a subclass is considered reused in the subclass.
In some cases, a subclass may customize or redefine a method inherited from the superclass. A superclass method which can be redefined in this way is called a virtual method.
4. Polymorphism
Polymorphism means one name, many forms. Polymorphism manifests itself by having multiple methods all with the same name, but slightly different functionality.
There are 2 basic types of polymorphism.
Overriding, also called run-time polymorphism. Method will be used for method overriding is determined at runtime based on the dynamic type of an object.
Overloading, which is referred to as compile-time polymorphism. For method overloading, the compiler determines which method will be executed, and this decision is made when the code gets compiled.
Answer3
public Members: If members are declared as public inside a class then such members are accessible to the classes which are inside and outside of the package where this
class is visible. This is the least restrictive of all the accessibility modifiers.
protected Members:If members are declared as protected then these are accessible to all classes in the package and to all subclasses of its class in any package where
this class is visible.
Default Members: When no accessibility modifier is specified for the member then implicitly it is declared as Default. These are accessible only to the other classes
in the class’s package.
private Members:This is the most restrictive of all accessibility modifiers. These members are accessible only with in the same class. These are not accessible from
any other class within a class’s package also.
Example:
package p2;
import p1.cPub;
public Class subcPub2 extends cPub {
vPub = 10; // public variable is accessible in subclass of another package
}
package p2;
import p1.cPub;
public Class cPubpack2 {
cPub subcPubObj = new cPub();
subcPubObj.vPub = 10; // public variable is accessible in another class of another package
}
Answer5:
In Java you do not need to use any keyword like virtual or override since by default all non static functions are considered as virual. You have to make it either
private or use final keyword to remove the default virtual feature in each function in the Java classes.
There are 3 classes given on this example. Shape is a base class and Circle and Triangle are the classes derived from the base class Shape. The base class Shape* is
used to access all two dervied class functions.
import java.io.*;
class Shape
{
public Shape()
{
System.out.println("Shape");
}
public void draw()
{
System.out.println("Drawing Shape");
}
}
class Circle extends Shape
{
int r;
public Circle()
{
System.out.println("Circle");
}
public void draw()
{
System.out.println("Drawing Circle");
}
}
class Triangle extends Shape
{
public int a,b,c;
public Triangle()
{
System.out.println("Triangle");
}
public void draw()
{
System.out.println("Drawing Triangle");
}
}
class VirtualFunctions
{
public static void main(String[] args)
{
Shape[] objects = { new Shape(), new Circle(),new Triangle()};
System.out.println("Now Drawing Objects");
for(int i = 0; i < 3; i++)
objects[i].draw();
System.out.println(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.