Why should I extend a class? [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Right now I am confused with inheritance and object of class. We can do the same using object as inheritance does.
ok here is first code
public class MainForm {
GUI g = new GUI();
g.show();
g.destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
now second one
public class MainForm extends GUI {
//GUI g = new GUI();
void show();
void destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
both code do the same.Right? then
Why should I extend a class when i can do the same using an object of class?

Inheritance is often used when you want to create multiple classes of a parent class, without duplicating a lot of logic. I may have a class named Animal which has methods speak(), walk(), and sleep(). I also may want to have specific instances of Animal like Cat and Dog.
Rather than implementing all three of those methods individually, I can write them in Animal and then have my other classes extend Animal to make use of those, as well as add any methods specific to those classes (like claw() or fetch()).
The benefits are reduced code duplication and better 'abstraction' of the objects. However, there are some drawbacks as well, as this article points out. It is important to know when inheritance is useful, and when it is not.

Related

What will happen if we have a private constructor in the class? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What will happen if we have a private constructor in the class?
(in Java)
It means that (without reflection) constructor wont be accessible outside of your class so other classes wont be able to call it. Only members of your class will be bale to create its object.
class A{
private A(){} //private constructor
private static A a = new A(); //you can create A object as a field
void test(){
new A(); // you can create A object inside methods of your class
}
class Inner{ // inner classes are also members of outer class so you can use
// A constructor here
A a = new A(); //OK
void test(){
new A(); //OK
}
}
}
class B{
A a = new A();//error: we don't have access to A constructor
}
You can only use that constructor within the current class.
You will only be able to call it from other constructors in the class, or from static methods in the class.
This is normally a way of restrict creation of object from this class. This means you cannot create object from the class.

What is the explicit constructor access modifier? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.
public - any one can call the constructor from anywhere in the code.
private - Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here
protected - Like private but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.
As #dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.

Accesing Private Methods in java? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how to implement such a functionality to access private members ?
Java checks access permissions during compilation only. Are you surprised? I was surprised very much to find out this fact.
So you can create skeleton of the third party class (even with empty implementations.). The interesting method should be protected instead of private. Now write your subclass and compile it against your stub. Then package only your subclass and try to run it with the "real" class. It should work.
I have tried it when I had to access private method or field and it worked fine for me.
ref. https://stackoverflow.com/a/4440051/1312423
Java checks access permissions during compilation only. Are you surprised?
Yes, because it checks the access modifier at runtime as well.
I start with
public class AnotherClass {
protected static void printMe() {
System.out.println("Hello World");
}
}
public class Main {
public static void main(String... args) {
AnotherClass.printMe();
}
}
and it compiles and runs.
If I change the printMe() to be private without re-compiling Main it does compile but when I run Main I get.
Exception in thread "main" java.lang.IllegalAccessError: tried to access method AnotherClass.printMe()V from class Main
at Main.main(Main.java:22)

Importing Classes and Inheriting a Class [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is just a small one but i just need more clarity on these two.
By import you tell the compiler that my program is going to use imported classes so please make them available.
import java.util
By inheriting class you are going to use class properties and functions (which are being inherited) in child class.
class Maruti extends Car{
}
import allows you to use the imported class in the class you're currently writing.
Inheriting, or using the extends keyword allows you to implement the current class with the functionality of the class you are inheriting from.
For instance:
public class Animal
{
public void walk()
{
System.out.println("i am walking");
}
}
public class Cat extends Animal
{
public void meow()
{
System.out.println("Meow!");
}
public static void main(String[] args)
{
Cat catAnimal = new Cat();
cat.walk();
cat.meow();
}
}
So as you can see in the above example, because Cat extends Animal the Cat class can do everything the Animal class can.
import lets you see the class so you can inherit (aka extend) it.
Importing classes are simply allowing your class to have access to these classes without using their fully qualified names.
For example, if you have the following:
import javax.swing.JFrame;
public class Main{
//this class has ACCESS to the JFrame class, but it isn't a JFrame because it doesn't inherit (extend) from one
}
Your Main class would have access to the methods and variables in the class javax.swing.JFrame without having to call it with that full name, it allows you to simply use JFrame.
Inheriting a class is extending your own class to gain access to a classes methods and variables because your class "is a" inherited class.
Here is some code that doesn't "import" JFrame, it uses its fully qualified name to extend itself so that it has access to every method/variable inside the JFrame class (as long as the modifier is public or protected).
public class MyFrame extends javax.swing.JFrame {
//this class IS a JFrame, and is not importing one, it's instead using the fully qualified name to extend itself and make it a JFrame.
}

Is it possible inner classes in java? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
class demo {
interface test {
}
}
is it possible in Java innerclasses?
I'm not sure if I exactly understand your question, but I think the answer is: Yes, a class can declare an inner interface.
Yes, inner classes are possible in Java.
In your code it looks like you're talking about interfaces though. Having inner interfaces are possible, too:
public class Demo {
public static interface Inner {
}
}
You can declare inner classes, too:
public class Demo {
private class Inner {
}
}
Syntactically interface inside a class is correct .
But it depends upon your usage. Logically it will be correct depending upon your usage.
You can have any enum, class, interface or annotation inside one another.
public interface A {
public #interface B {
public class C {
public enum D {;
public interface E {
// keep on going.
}
}
}
}
}

Categories