Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am going through some past exam papers and some questions are pretty basic. One of the Question is;
which of the following best describes the role of a class in the Java language?
a) A class defines where a Java program starts executing.
b) A class is a graphical user interface component.
c) A class is a specification of an object, through grouping variables and methods that define that object.
d) A class is an instance of an object containing variables and methods.
e) A class is a group of IT students in the Computing Building at the university.
I personally believe that it's 'C' but then again 'D' also seems like the role of a class(my apologies if i am wrong). I am new to Java so i'm sorry if this seems like an unprofessional question. Thanks for your help.
The answer here is C.
Think of class as a blueprint for an object.
That is, if you are building a house, you're going to have a blueprint for it. The blueprint is the class, and the house is the object.
The class will also have "appropriate" and relevant fields and methods.
For example, if you have a class named Apple, you don't want to have a method something like getHorsePower.
A class is a specification of an object, through grouping variables
and methods that define that object.
And, basically, it's a blue print of it's objects.
a) A class defines where a Java program starts executing.
INCORRECT: It depends on where main() is located.
b) A class is a graphical user interface component.
INCORRECT: To create GUI multiple classes are required.
c) A class is a specification of an object, through grouping variables and methods that define that object.
CORRECT:
d) A class is an instance of an object containing variables and methods.
INCORRECT: Object is an instance of class not vice versa.
e) A class is a group of IT students in the Computing Building at the university.
INCORRECT: Unrelated to Java
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Java.
I was reading about Encapsulation concept in Object Oriented Programming.
While reading, I saw a line telling that :
If constructor of a class is declared as private, it will cause some
problems.
But it didn't tell what kind of problems could occur.
Can anyone tell what kind of problems are there with private constructor ?
When you declare a constructor of a class as 'private', you will not be able to create new instances "objects" of that class.
This is a problem if you do want to create new instances of the class, but this implementation is useful when making a Singleton Design Pattern.
If you are interested in knowing more about design patterns, I can share some resources with you.
Here is a book by Carlos E. Otero that covers the topic of design patterns:
https://books.google.com.tr/books?id=IL6FBLJn69UC&printsec=frontcover&redir_esc=y#v=onepage&q&f=false
Simple words, object or instance can't be created for that class.
As soon as, you write this statement below. For example
SomeClass c = new SomeClass(); // this will give an exception.
Basically, private constructors are used for making singleton classes.
On declaring a constructor as private you can't instantiate the class using default constructor. So, you need to create a constructor with public to access it outside.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to decide where is the best place to put my comparator class on a Java project.
public class CustomerSortingComparator implements Comparator<Customer>{
#Override
public int compare(Customer customer1, Customer customer2) {
//... my implementation
}
}
In my project I have several packages:
com.test.dao
com.test.model
com.test.controller
com.test.service
com.test.util
.....
I was thinking to add it to the util package since I can't find a good reference that suggests where to put it.
It’s clearly opinion-based, so I am hurrying to air mine. :-)
If your comparator is to be used in one place only, implement it where it is used. Inside the same class or at least in the same package. The design principle of high cohesion dictates that this is where it belongs. In particular because from Java 8 comparators can often be defined in one or very few lines of code, there is no point in putting it in its own class. Just declare it in the class or even inside the method using it:
Comparator<Customer> customerSortingComparator = Comparator.comparing(Customer::getLastName)
.thenComparing(Customer::getFirstName);
If on the other side the comparator is foreseen to be used in more than one class in more than one package, declare it in the package holding the Customer class. Again, the principle of high cohesion will tell you that this is the place.
Real life story: In the project I am working on they have put all the comparators that compare customers (for the sake of the example) into one class in a package separate from the Customer class and also separate from the classes using those comparators. It has given us a class with a non-existent cohesion and a lot of unnecessary coupling across packages. A very poor design.
Link: Cohesion (computer science) on Wikipedia
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Oracle Java Tutorials, Multiple Inheritance of State, Implementation, and Type:
One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field?
Couldn't this problem be easily solved by assigning priorities to all the classes a subclass extends?
class A extends B,C,D
{
}
Let us assume that priority was assigned in the ascending order in which they are mentioned in declaration of subclass .
Then priority of B < priority of C < priority of D . So if there is any state or behaviour common to any of these classes B ,C or D then priority decides what should be inherited and what should be hided.
Please advise. Thanks in advance.
Yes, roughly speaking, this how Scala supports extending a class with multiple Traits
"priorities" are determined based on a linearization equation, similar to what you mentioned so there is no ambiguity.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two classes(not interface).
Suppose the classes be class A and class B.
I want to know which is more efficient and correct implementation.
I want to access the method of A from B.
Two ways:
1) class B extends A (inherits)
2) In class B:
create an object/instance of Class A, and then access the method of class A.
Which one is more efficient and correct ??
It depends. You should only use extends (for good OOP) if it is logically correct. The way I learned is with the "is a" test. If saying B "is a" A then extends would suit.
For example:
Take 3 classes Dog, Canine and Fish
Dog "is a" Canine: this is logical and passes the "is a" test so we can use extends here
Dog "is a" Fish: this is not logical and should not inherit
If your class B is a logical subclass of A then go with extends.
You should read some documents about OOP (Object Oriented Programming). As you can read in the comments I would agree that it depends on your UseCase. A common rule for designing Objects is the 'Has a' and 'Is a' relations. This means if you can say MyObject 'Has a color' then You will add a color member to your Object as a property, if you can say MyObject 'Is a' bird then you will inherit - maybe - from Object MyAnimal that you have written before. There is a third way what you can do. If your method is a pure helper method - thus it does not own a sate while it is running, which means the method is defined within an object but does not use volatile members of that object for calculation - you could write a public static method and access it by MyObject.myStaticMethod(String myArgument). You have to be carefully with this kind of methods because if you don't know how to use them wright you can run into multiple problems e.g. when writing test or if you have a clustered environment.
But for now this should be fine. So make your objects granular over their properties and functionality. Methods that are not related to your object's type should be placed in Other objects that bundle this functionality - e.g. printString functionality in a printer Object- or if the methods are stateless then put them in a static statless helper object that bundles the functionality you need.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Can someone please explain to me how to implement these these types of code and explan what the difference between the three are? I am coding in java.
I'm goign to take a shot at it because I recently tried to understand those and this is a good way to see if I did :) because If you can't explain something you haven't really understood it :)
Casting is fairly simple. It means to pretty much convert a value or object of a certain type to a different type. This way you can for example turn a float into an integer
float y = 7.0
int x = (int) y
x will now be 7.
Of course you can't simply cast any type to any other type. There are limitations which you should search for on google - i could never cover all of them.
Polymorphism sounds similar but is actually something else. As I understand it it means that certain objects can be of multiple types. For example of you have a class that extends another class any instance of the parent class can also be of the type of the derived class.
class Base {...}
class Derived extends Base {...}
Base obj1 = new Base();
Derived obj2 = new Derived();
obj1 = obj2;
Over the course of this snippet obj1 will have been an instance of Base first but then it will be an instance of Derived which is a class derived from base. This is possible because instances of derived classes contain an "inner object" (i don't know the official name) of the base class. When you cast the Base instance to an instance of Derived you will actually get this "inner object"
Hope this helps
refer to the documentation from Oracle : http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
http://docs.oracle.com/javase/tutorial/java/IandI/index.html
True polymorphism (i.e., multiple inheritance) is not available in Java. However, you can get a good approximation using "Interfaces", though your classes need to implement all of the functions provided by the interface (link to Java Interfaces).
You can also emulate multiple inheritance using delegated setters/getters on classes. This can be complicated, but it can also give you the effect of multiple inheritance.
This topic is discussed at length in this Stack Overflow post.