Section 1

Preview this deck

Member variables

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (25)

Section 1

(25 cards)

Member variables

Front

refers to both the class and instance variables that are defined by a particular class

Back

Interface vs Abstract Class

Front

1) Interface in Java can only contains declaration. You can not declare any concrete methods inside interface. On the other hand abstract class may contain both abstract and concrete methods, which makes abstract class an ideal place to provide common or default functionality. 2) Java interface can extend multiple interface also Java class can implement multiple interfaces, which means interface can provide more Polymorphism support than abstract class . By extending abstract class, a class can only participate in one Type hierarchy but by using interface it can be part of multiple type hierarchies. 3) In order to implement interface in Java, until your class is abstract, you need to provide implementation of all methods, which is very painful. On the other hand abstract class may help you in this case by providing default implementation. 4) One more general rule of when to use abstract class and interface is to find out whether a certain class will form a IS-A hierarchy or CAN-DO-THIS hierarchy. If you know that you will be creating classes e.g. Circle, Square than it's better to create an abstract class Shape which can have area() and perimeter() as abstract method, rather than defining Shape as interface in Java. On the other hand if you are going to create classes which can do thinks like, can fly, you can use interface Flyable instead of abstract class.

Back

Association

Front

Association is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with a single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Back

Why is the main method static?

Front

To access a static method class object is not needed. The method can be accessed directly with the help of ClassName. So when a program is started the jvm search for the class with main method and calls it without creating an object of the class.

Back

Inheritance v. Polymorphism

Front

- Inheritance defines parent-child relationship between two classes, polymorphism takes advantage of that relationship to add dynamic behavior in your code. - Inheritance encourages code reusability by allowing child class to inherit behavior from the parent class. On the other hand Polymorphism allows child to redefine already defined behaviour inside the parent class. Without Polymorphism it's not possible for a child to execute its own behaviour while represented by a Parent reference variable, but with Polymorphism it can be done. - Java doesn't allow multiple inheritance of classes, but allows multiple inheritance of Interface, which is actually required to implement Polymorphism. For example, a class can be Runnable, Comparator and Serializable at the same time because all three are interfaces. This makes them pass around in code e.g. you can pass an instance of this class to a method which accepts Serializable, or to Collections.sort() which accepts a Comparator.

Back

Class variables or static member variables

Front

static variables that are shared by every instantiation of a class public static int var_name;

Back

Method Overriding

Front

When a child class overwrites a method inherited from a parent class.

Back

Class methods

Front

belong to the class as a whole and have access only to class variables and inputs from the procedure call. static method

Back

Aggregation

Front

Aggregation is a specialized form of Association where all objects have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not destroy. We can think about "has-a" relationship.

Back

Class

Front

a collections of attributes and methods defining the behaviour of an object.

Back

inheritance

Front

Inheritance allows a Child class to inherit properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

Back

Abstraction

Front

Abstraction is a way of converting real world objects in terms of class. It's a concept of defining an idea in terms of classes or interface. You cannot create an instance of an abstract class

Back

Encapsulation

Front

The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how things work and just exposing the requests a user can do. (so that changing the private methods is very flexible) - All methods not called outside of the class they are defined in should be private - all member variables are made private so they are well encapsulated you can only change or access this variable directly inside this class. if you want to allow outside world to access these variables is better creating a getter and setter Advantages of encapsulation: 1. Encapsulated Code is more flexible and easy to change with new requirements. 2. Encapsulation in Java makes unit testing easy. 3. Encapsulation in Java allows you to control who can access what. 4. Encapsulation also helps to write immutable class in Java which is a good choice in multi-threading environment. 5. Encapsulation reduces coupling of modules and increases cohesion inside a module because all piece of one thing is encapsulated in one place. 6. Encapsulation allows you to change one part of code without affecting other parts of code.

Back

method overloading

Front

Overloading is determined at the compile time. It occurs when several methods have same names with: Different method signature and different number or type of parameters. Same method signature but the different number of parameters. Same method signature and same number of parameters but of different type

Back

Instance methods

Front

belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables

Back

Immutable Class

Front

A class is said to be Immutable if its state cannot be changed once created, for example, String in Java is immutable. Once you create a String say "Java", you cannot change its content. Any modification in this string e.g. converting into upper case, concatenating with another String will result in the new object. An immutable object is very useful for concurrent programming because they can be shared between multiple threads without worrying about synchronization. In fact, the whole model of functional programming is built on top of Immutable objects.

Back

Composition

Front

It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

Back

Polymorphism

Front

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. It could be as simple as inheritance where child class can add more methods to add behavior in addition to the ones inherited from its Parent. keyword "extends"

Back

What is object-oriented programming?

Front

a programming paradigm formulated around Objects which can hold attributes defining it behavior. The data of the objects can be modified by the methods.

Back

Aggregation, Composition, Association

Front

Association means two objects are related to each other but can exist without each other, Composition is a form of association where one object is composed of multiple objects, but they only exists together e.g. human body is the composition of organs, individual organs cannot live they only useful in the body. Aggregation is a collection of object e.g. city is an aggregation of citizens.

Back

Override vs Overload

Front

Overriding is resolved at runtime while overloading is compile time. Also, rules of overriding and overloading are different, for example in Java, method signature of the overloaded method must be different than original method, but in the case of overriding it must be exactly same as an overriding method.

Back

multiple inheritance

Front

If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes. The problem with the multiple inheritance is that if multiple parent classes have methods with same name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class. To overcome this problem java allows to implement multiple Interfaces.

Back

Things to note about abstract classes in Java

Front

1) Use abstraction if you know something needs to be in class but the implementation of that varies. Abstraction is actually resulting of thought process and it really need good experience of both domain and Object oriented analysis and design to come up with good abstraction for your project. 2) In Java, you can not create an instance of the abstract class using the new operator, its compiler error. Though abstract class can have a constructor. 3) abstract is a keyword in Java, which can be used with both class and method. Abstract class can contain both abstract and concrete method. An abstract method doesn't have the body, just declaration. 4) A class automatically becomes abstract class when any of its methods declared as abstract. 5) abstract method doesn't have method body. 6) In Java, a variable can not be made abstract , its only class or methods which would be abstract. 7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. alternatively, this class can also be abstract.

Back

instance variables / attributes

Front

data that belongs to individual objects; every object has its own copy of each one public int var_name;

Back

Object

Front

Instance of a class having the values assigned to the properties of the class. Each object is said to be an instance of a particular class. Procedures in object-oriented programming are known as methods; variables are also known as fields, members, attributes, or properties.

Back