COMPSCI4039
Programming IT
Revision – I
Copyright By PowCoder代写 加微信 powcoder
• A class is a series of instructions.
• We don’t tend to use classes directly.
• Objects are instances of a class.
• We use objects to achieve something in Java.
• The principle of classes is to group some data and behaviour together when conceptualising objects.
• e.g. Scanners are objects that have data and behaviour to allow us to acquire input from users.
• In OO languages like Java, objects are used all the time!
• Here we have a class representing a ‘Home’
• We leverage on this class to create Home objects.
• Attributes are specified in the class which become instance variables in objects.
• Attributes = data that objects have.
• Methods specified in the class are also available to objects.
• Combination of instance variables and methods are building blocks of any modern software program.
• Objects are stored in memory.
• At a specific memory address.
• A variable, is a reference that points to that address.
• Classes are used to create objects, memory is used to store objects.
• When you do anything with an object variable, you are using a handle that points to the physical object location in RAM.
Constructors
• Constructors can have signatures, which basically tell Java different ways how to create an object.
• Home has 2 constructors:
• One which doesn’t take any input parameters.
• Another which takes a person object as an input parameter
• A signature, is essentially the input parameters to a constructor method.
• Instance variables are pieces of data that are held locally by an object.
• These are specified as attributes within a class file.
• Each object of that class will have a local value associated with the attribute.
• Hence why they are known as instance variables.
• Here there are 3 attributes which in turn create 3 instance variables held by each Home object.
• Instance variables are pieces of data that are held locally by an object.
• These are specified as attributes within a class file.
• Each object of that class will have a local value associated with the attribute.
• Hence why they are known as instance variables.
• Here there are 3 attributes which in turn create 3 instance variables held by each Home object.
Static vs non-static
• Methods are used on objects when they are created.
• Data that is held by an object is local to that object.
• Static methods and attributes belong to a class, not an object. • Objects can, however, access them via the class.
• There is one static attribute associated with the Home class.
• This value (initially 0) is not associated with any object that is created from this blueprint.
• It is a value attached to the class directly.
• We use static attributes when there is some data that we know that will be the same for every object of that class.
• e.g. every human is a homo Sapien, therefore this is a static attribute that defines every human object, including you and me.
• However, our names are local, your name may not be the same as mine. Therefore, it is held as an instance variable by a human object.
• When we create a home object, first we call the constructor
• Next, we update the static attribute. This means the id attribute associated with the Home class is now 1.
• Then, we copy this value (as it is a primitive) by having uniqueID point to the static attribute.
• This process is repeated for home2
• And again, for home3
Inheritance
• Classes can be specified in a hierarchy. • This allows us to save code.
• A Home for example can be one of several things: • An apartment.
• A caravan
• A house in turn can be: • Detached
• Semi-Detached
Inheritance Rule
• The relation between a super and sub class is one way.
• A super class cannot access attributes or methods that are specified
by the sub class.
• Think of it like this…
• Bird is a super class.
• Bird has an attribute legs = 2;
• Penguins are a sub class of birds.
• Penguins have an attribute fly = false;
• Penguins have 2 legs; they get this from the super class.
• However, Bird should not access fly = false, as most birds fly.
A note on inherited methods
• If a class prescribes the method helloWorld() and it is public… • We cannot change it to private or protected in any sub class.
• You cannot reduce the visibility of that method.
• You can however increase the visibility of that method.
• This only applies to methods that are being overloaded by polymorphism!
Polymorphism
• The idea with polymorphism is to dynamically change what a method does, depending on what calls it.
• Consider if you were making a game.
• Each creature in that game has a light and a heavy attack.
• Polymorphism allows you to change what the value of the attack is for example, without needing to write more methods.
• Polymorphism, like inheritance, allows us to reduce the code that we write.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com