Partial 1 masteries

What is an object?

Something that has state and behavior, state can be things to describe the object. (Color, shape, weight, etc.) And behavior is what the object does (Running, talking, walking, etc.)

What is an attribute?

Attributes are the state of the objects, are things who will describe and make your object. If you had a cat object, your attributes would be its size, weight, color, name, etc.

What is a method?

Methods are the behavior of the object, they tell the objects what to do and how to do it. For example if you had a cat object, then the methods would be Walking, meowing, eating, etc.

What is abstraction?

Abstraction in object oriented programming is basically giving information on what the object does, but not how it does it.
Abstract classes don’t need to have abstract methods, but if a class has 1 or more abstract methods, then it needs to be declared as an abstract class.
To use an abstract class you don’t instantiate it, you inherit it from another class, and you need to provide implementation to all the abstract methods it has.

What is encapsulation?

Is wrapping the data and the code that uses said data (methods) together. When you encapsulate a class, its variables will be hidden from other classes and can only be accessed through methods of their current class. To achieve encapsulation you must declare the variables as private and provide public setter and getter methods to modify and view the variables.

What are “has-a” and “is-a” relationships?

“Has-a” relationship is basically when you describe an object, for example a Cat has 4 legs, a name, color, size and weight.

Meanwhile, a “is-a” relationship is when you categorize something, for example a cat is a mammal. You have to realize this only goes one way, for example, every cat is a mammal, but not every mammal is a cat.

What are the mechanics for class description (coding) in the JavaProgramming Language?

You have to remember that classes cannot be private, so when creating a class, it should look like this:

public class ClassName(){

}

note that the class name has no spaces and usually uses capital letter to indicate the start of a new word.

What are visibility modifiers, their purpose and use in practice?

There are basically 4 visibility and access modifiers in Java:
Default
Private
Public
Protected

The first one, the default one doesn’t use any keyword. And what it does is a variable or method declared without any modifier is only available to classes in the same package.

Private is the most restrictive, it makes variables and methods only available to the class they are declared on, classes and interfaces can’t be private.

Public is the most open one, a variable or method can be accessed by any other class. But if the class we are trying to access is in a different package, it needs to be imported.

Protected basically allows any subclasses to access the variables and methods in a superclass.

What are inheritance and polymorphism? (Specification and implementation)

Inheritance is the process where one class acquires the properties (methods and fields) of another. The information is managed in hierarchical order.
The class which inherits the properties of other is known as subclass, and the class whose properties are inherited is known as superclass. It uses the “extends” keyword.

class Mammal{

}

class Cat extends Mammal{

}

Polymorphism is the ability of an object to take many forms, the most common use is when a parent class reference is used to refer to a child class object.
Any object that can pass more then one “is-a” relationship is considered polymorphic.

public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}

By this logic, Deer is-a animal, vegetarian, deer and object.

Leave a comment