oop - Can an instance variable belong to multiple classes? -
i have been looking @ classes in oop , curious how works.
for example, lets have product. product bike.
there class product stock , class products details e.g. colour etc.
i use classes within each other such class called product , within it contains both stock , details product(bike)
or have instance called bike made both
e.g.
stock bike = new stock(); details bike = new details();
or instance bike have renamed them e.g.
stock stockbike = new stock(); details detailsbike = new details();
hopefully makes sense. wondered whether 1 instance keyword can belong 2 classes or instance name in case bike have changed can belong more classes.
thanks
stock bike = new stock(); details bike = new details();
not quite sure you're asking... think you're asking if can have 2 variables same name , in same scope. generally, no. didn't mention language let's assume java.
public void test() { string test = "123"; string test = "123"; // error integer test = 123; // error stock bike = new stock(); details bike = new details(); // error }
one case have same name... although discouraged... local variable same name , type variable of higher scope
public class test { private string mystring; ... public void test() { string mystring = "123"; // warning , discouraged } }
Comments
Post a Comment