Question

Explain the following object-oriented(OO) concepts with the aid of code examples (either C++ or Java): Inheritance Over-ridin

3(c) 17 Marks] Describe C++ namespaces using a code example. Describe Java packages, again using a code example. How do C++ n

Explain the following object-oriented(OO) concepts with the aid of code examples (either C++ or Java): Inheritance Over-riding . Over-loading Describe any differences between C++ and Java in how these OO concepts are implemented?
3(c) 17 Marks] Describe C++ namespaces using a code example. Describe Java packages, again using a code example. How do C++ namespaces compare to Java packages? 3(d) [5 Marks] What are inline methods in C++/Java? Explain the terms accessor and mutator.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Inheritance :

Inheritance is one of the best feature or concept in which child class (Derived (c++)/sub class(Java)) accesses the
properties of parent class (Base(c++)/Super class(Java)) .

In java There are 3 type of inheritance while in C++ it has 5 types.

C++ :

Single Inheritance : One Base class & One Derived class
Multilevel Inheritance : One base class ,One derived class & again sub derived class and so on
Hierarchical Inheritance : One base and multiple derived classes
Multiple Inheritance : Multiple Base classes but single child class
Hybrid Inheritance : a combination of two ineritances like a combination of hierarchical and multiple inheritance

Java :

Single Inheritance : One super class & One sub class
Multilevel Inheritance : One super class ,One sub class & again subclass has sub class and so on
Hierarchical Inheritance : One super class and multiple sub classes

Java has only three types because in java a child has only one parent means Multiple parents are not allowed in java

Example of Inheritance in java :

Two classes Employee(Super class) & Manager (Sub class)


class Employee
{
int eno;
String enm;

Employee(int no,String nm)
{
eno=no;
enm=nm;
}

void empInfo()
{
System.out.println("Employee No : "+eno);
System.out.println("Employee Name : "+enm);
}
}

class Manager extends Employee
{
int month ,rate;

Manager(int no,String nm,int r,int m)
{
super(no,nm);
rate=r;
month=m;
}

void salaryInfo()
{
super.empInfo()
System.out.println("Salary is "+(rate*month));
}
}

class Test
{
public static void main(String args[])
{
Manager m1=new Manager(101,"Pravin",50000,1);
m1.salaryInfo();
}
}


Overloading :

An Overloading is process/ compile time polymorphism in which one class has many functions/methods with same name but different signatures.
Overloading concept is similar in c++and java even implementation .(except syntax)

Example in java :

class Overloading
{

void addition(int a,int b)
{
System.out.println("Addtion of two integers : "+(a+b));
}

void addition(double a,double b)
{
System.out.println("Addtion of two doubles : "+(a+b));
}
}

class Test
{
public static void main(String args[])
{
Overloading o1=new Overloading();
o1.addition(10,20);
o1.addition(35.50, 640.12);
}
}


Overriding :

An Overloading is process/ run time polymorphism in which many classes have one function/method with same name and with same signatures.
Overriding concept is similar in c++and java even implementation .(except syntax)

Example in Java :

abstract class A
{
void addition(int a,int b);
}

class B extens A
{
void addition(int a,b)
{
System.out.println("Addition is "+(a+b));
}
}

class Test
{
public static void main(String args[])
{
B b1=new B();
b1.addition(100,200);
}
}


3 (c)
Namespace in C++ & Package in Java
:

Namespace in C++ :

-> A namespace is a containter for identifiers or declarative region that provides to a scope to the identifiers.

Simple Example of Namespace :

#include<iostream>
using namespace std;

namespace A
{
void demo()
{
cout<<"First method of the program!!";
}
}

namespace B
{
void demo()
{
cout<<"Second method of the program!!"
}
}


int main()
{
A::demo();
B::demo();
return 0;
}

Package in java :

A package is a container or a collection of classes,interfaces or sub directives.

Example :

package p1;

public class A
{
public void addition(int a,int b)
{
System.out.println("Addition of two numbers is "+(a+b));
}
}

package p2;
import p1.A;

class Test
{
public static void main(String args[])
{
A a1=new A();
a1.addition(100,200);
}
}
namepaces are nested while packages are flat.


3(d)
Inline methods:

A method with one or two line code for efficient or for fast execution that method is known as Inline method.

Example :

#include<iostream>

inline int addition(int a,int b)
{
return(a+b);
}

int main()
{
cout<<"Addition of two numbers is "<<addition(1,2);
return 0;
}

Accessor & Mutator :

Accessot and mutator are functions to set and get the values of the variables.

Example :

#include<iostream>
using namespace std;

class Demo
{
public:
int a;
void set(int a1) //accessor
{
a=a1;
}

int get() //mutator
{
return a;
}
};

int main()
{
Demo d1;
d1.set(100)

cout<<"Value of given variable is "<<d1.get();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Explain the following object-oriented(OO) concepts with the aid of code examples (either C++ or Java): Inheritance Over-riding . Over-loading Describe any differences between C++ and Java in how the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game...

    C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows: class Creature { private: int type; // 0 Human, 1 Cyberdemon, 2 Balrog, 3 elf int strength; // how much damage this Creature inflicts int hitpoints; // how much damage this Creature can sustain string getSpecies() const; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT