Objects and Classes In Java and Clike languages

Objects and Classes In Java and C-like languages by Diana Stoeva and Milan Iliev

Structs A package deal! A struct* is a type of value made of other values: Capt. Marvel first. Name "Carol" last. Name "Danvers" alias "Mar-Vell" height 170 Capt. Marvel. first. Name Capt. Marvel. last. Name Capt. Marvel. alias Capt. Marvel. height *Java doesn't have structs, it just uses objects instead // // "Carol"; "Danvers"; "Mar-Vell"; 170; by Diana Stoeva and Milan Iliev

Structs Defining a Struct template Defining a Struct in C++ looks like this: Capt. Marvel first. Name "Carol" last. Name "Danvers" alias "Mar-Vell" height 170 struct Person { string first. Name; string last. Name; string alias; int height; }; Person Capt. Marvel; Capt. Marvel. first. Name Capt. Marvel. last. Name Capt. Marvel. alias Capt. Marvel. height = = "Carol"; "Danvers"; "Mar-Vell"; 170; by Diana Stoeva and Milan Iliev

Objects Structs with Functions! Capt. Marvel An object is just a struct with functions! first. Name "Carol" last. Name "Danvers" alias "Mar-Vell" height 170 full. Name Function() crack. Joke Function() Capt. Marvel. first. Name Capt. Marvel. last. Name Capt. Marvel. alias Capt. Marvel. height // // "Carol"; "Danvers"; "Mar-Vell"; 170; Capt. Marvel. full. Name() // "Carol Danvers" Capt. Marvel. crack. Joke() // "Eat your sandwich. " by Diana Stoeva and Milan Iliev

Classes They're object templates. To define an object in Java, first, define a class for it: Capt. Marvel first. Name "Carol" last. Name "Danvers" alias "Mar-Vell" height 170 full. Name Function() crack. Joke Function() class Person { String first. Name; String last. Name; String alias; int height; public String full. Name(){ return first. Name + " " + last. Name; } public String crack. Joke(){ return "Eat your sandwich. "; } } Person Capt. Marvel = new Person(); by Diana Stoeva and Milan Iliev

Defining Classes class Name. Of. Class { int a. Variable; String another. Variable; public void function. Name(){ // code of function } public String another. Function(){ // code of function } } Name. Of. Class a. Variable = new Name. Of. Class(); by Diana Stoeva and Milan Iliev

Instance variables An object of a class is called an instance of that class. Variables that are part of an object are called instance variables. class Vegetable { String name; public void speak(){ System. out. println("I. Am. " + name + "!"); } Vegetable potato = new Vegetable(); potato. name = "Potato"; potato. speak(); // prints "I. Am. Potato!" tomato. speak(); // prints "I. Am. Tomato!" Function() name "Potato" potato } Vegetable tomato = new Vegetable(); tomato. name = "Tomato"; speak tomato name "Tomato" speak by Diana Stoeva and Milan Iliev

Class Variables A class can also have variables. The variables in a class are declared using the static* keyword in Java. *it's a holdover from C++ class Vegetable { String name; static String line = "I. Am. "; public void speak(){ System. out. println(line + name + "!"); } } Vegetable potato = new Vegetable(); potato. name = "Potato"; Vegetable tomato = new Vegetable(); tomato. name = "Tomato"; potato. speak(); // prints "I. Am. Potato!" tomato. speak(); // prints "I. Am. Tomato!" Vegetable line "I. Am. " speak Function() name "Potato" potato speak line by Diana Stoeva and Milan Iliev

Naming Convention In Java, names are typically written with Inter. Caps to separate letters. Variables and methods are typically named starting with lowercase letter: int age = 5; String adorable. Kitten. Name = "Snickers"; Vegetable potato = new Vegetable(); public add. Two. Numbers(…){ … } Classes are typically named starting with uppercase letter: class Vegetable { … } new Vegetable() by Diana Stoeva and Milan Iliev

this In Java, there are three kinds of variables: local variables, instance variables, and class variables. So how do you tell them apart? class Person { String first. Name; … public say. Hello(String first. Name){ System. out. println("Hello, " + first. Name + ", I am " + first. Name + ". "); } … There is an explicit way to refer to the object the method is a part of - the this keyword. class Person { String first. Name; … public say. Hello(String first. Name){ System. out. println("Hello, " + first. Name + ", I am " + this. first. Name + ". "); } … by Diana Stoeva and Milan Iliev

this is like an automatically-defined local variable that always refers to the current object. It works exactly like you'd expect: class Person { String first. Name; … public say. Hello(Person other){ System. out. println("Hello, " + other. first. Name + ", I am " + this. first. Name + ". "); } … Person alice = new Person(); alice. name = "Alice"; Person bob = new Person(); bob. name = "Bob"; bob. say. Hello(alice); // prints "Hello Alice, I am Bob. " by Diana Stoeva and Milan Iliev

this So how does all of this work? public say. Hello(Person other){ System. out. println("Hello, " + other. first. Name + ", I am " + this. first. Name + ". "); } bob bob. say. Hello(alice): alice. say. Hello(bob): this bob this alice other bob. say. Hello(bob): this bob other bob by Diana Stoeva and Milan Iliev

this So why can we also say just first. Name instead of this. first. Name? When a variable name is used, Java checks the following: • Local Variables • Instance Variables • Class Variables So, if there is a local variable named first. Name, Java will use that. If there isn't, it will look for an instance variable, and a class variable *: class Person { String first. Name; … public say. Hello(String first. Name){ System. out. println("Hello, " + first. Name + ", I am " + this. first. Name + ". "); } … If both a local and an instance variable exist, it's said that the local variable shadows the instance variable. by Diana Stoeva and Milan Iliev

Constructors In order to create a Person object, you have to supply a lot of data. Also, you might need to perform some code to set up the object. Person Capt. Marvel = new Person(); Capt. Marvel. first. Name Capt. Marvel. last. Name Capt. Marvel. alias Capt. Marvel. height = = "Carol"; "Danvers"; "Mar-Vell"; 170; It would be a lot easier if you could specify all those values when creating a Person. So it's a one-step process instead of a five-step one. Person Capt. Marvel = new Person("Carol", "Danvers", "Mar-Vell", 170); Okay, but how? by Diana Stoeva and Milan Iliev

Constructors When defining a class, you can define a method that's automatically called when an instance of the class is created. That method is called a constructor. class Person { String first. Name; String last. Name; String alias; int height; public Person(String first. Name, String last. Name, bool is. Iron. Man){ int height = Math. random() * 200; this. first. Name = first. Name; this. last. Name = last. Name; this. height = height; if(is. Iron. Man){ this. alias = "Iron Man"); } … Constructors work like any other method, except you don't call them directly, and they're named after the class they're defined for. Person Capt. Marvel = new Person("Carol", "Danvers", false); by Diana Stoeva and Milan Iliev

Method Overloading A Java class may have multiple methods with the same name, but different arguments: class Person { … public say. Hello(String first. Name){ System. out. println("Hello, " + first. Name + ", I am " + this. first. Name + ". "); } public say. Hello(Person other){ System. out. println("Hello, " + other. first. Name + ", I am " + this. first. Name + ". "); } … In this case, both of these work: bob. say. Hello(alice); bob. say. Hello("Charlie"); // prints "Hello Alice, I am Bob. " // prints "Hello Charlie, I am Bob. " This works for constructors too! A class can have more than one constructor, as long as they have different arguments. by Diana Stoeva and Milan Iliev

Public and Private Methods A class' methods and variables can be declared as public or private. class Person { … public say. Hello(Person other){ System. out. println("Hello, " + other. first. Name + ", I am " + this. first. Name + ". "); } private say. Goodbye(Person other){ System. out. println("Goodbye, " + other. first. Name + ", I am " + this. first. Name + ". "); } Outside the Person class: bob. say. Hello(alice); bob. say. Goodbye(alice); // prints "Hello Alice, I am Bob. " // error: cannot access bob's say. Goodbye() method outside a Person Within the Person class, however, both methods can be called freely. by Diana Stoeva and Milan Iliev

Public and Private Variables Public and private instance and class variables work the same way. class Person { public String name; private String a. Secret; Outside the Person class: bob. name bob. a. Secret // returns "Bob" // error: cannot access bob's a. Secret variable outside a Person (If neither public or private is specified, variables and methods are automatically public to other classes in the same package but private to everything else. ) Local variables can't be public or private - they don't exist outside their method anyway. by Diana Stoeva and Milan Iliev
- Slides: 18