Programming Articles

Page 2538 of 2545

Can a constructor be overridden in java?

Sai Subramanyam
Sai Subramanyam
Updated on 30-Jul-2019 16K+ Views

If super class and sub class have same methods including name, return type and parameters, and if you try to call it using the object of the sub class Then the method in the sub class is invoked. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class’s constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error. Example ...

Read More

Can access modifiers be used for local variables in Java?

Arushi
Arushi
Updated on 30-Jul-2019 856 Views

Yes, a local variable can be public, private, protected or default.

Read More

How many ways a String object can be created in java?

George John
George John
Updated on 30-Jul-2019 1K+ Views

You can create a String by − Step 1 − Assigning a string value wrapped in " " to a String type variable. String message = "Hello Welcome to Tutorialspoint"; Step 2 − Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor. String message = new String ("Hello Welcome to Tutorialspoint"); Step 3 − Passing a character array to the String constructor. char arr[] = {'H','e','l','l','o'}; String message = new String(arr);

Read More

What is the difference between a String object and a String literal in Java?

Paul Richard
Paul Richard
Updated on 30-Jul-2019 1K+ Views

When the String literal used to create String, JVM initially checks weather String with the same value in the String constant pool, if available it creates another reference to it else it creates a new object and stores it in the String constant pool.In case of an object, each time you instantiate the class a new object is created given value irrespective of the contents of the String constant pool.

Read More

Can interfaces have constructors in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 796 Views

No, interfaces can’t have constructors for the following reasons − All the members of an interface are abstract, and since a constructor cannot be abstract. Still, if you try to write a constructor within an interface it will generate a compile time error. Example public interface InterfaceTest { InterfaceTest(){ } public abstract void display(); public abstract void show(); } Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected public InterfaceTest(){ ^ 1 error

Read More

Can constructors be inherited in Java?

Monica Mona
Monica Mona
Updated on 30-Jul-2019 4K+ Views

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors. Example public interface InterfaceTest { public InterfaceTest(){ } public abstract void display(); public abstract void show(); } Still, if you try to write constructors in an interface it will generate a compile time error. Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected InterfaceTest(){ ^ 1 error C:\Sample>

Read More

What is the difference between a String object and a StringBuffer object in java?

Rishi Raj
Rishi Raj
Updated on 30-Jul-2019 468 Views

String class is immutable once you create an object of string you can modify its data.The StringBuffer class is immutable, once you create a StringBuffer object you can change/modify the contents of it.This class provides various methods to manipulate its data such as append(), delete(), insert() etc.

Read More

How many public classes of the same name it can have in Java?

Swarali Sree
Swarali Sree
Updated on 30-Jul-2019 2K+ Views

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error. Example public class Example { } public class Example{ public void sample(){ System.out.println("sample method of the Example class"); } public void demo(){ System.out.println("demo method of the Example class"); } ...

Read More

What are the prerequisites for learning Java?

vanithasree
vanithasree
Updated on 30-Jul-2019 2K+ Views

In fact, you can directly start learning Java without any prior knowledge of programming language. But, the syntax in Java is similar to the syntax of the C programming language, therefore, Knowing C language helps to get hold of Java quickly. Having introduced to object-oriented principles before starting Java, also helps in the understanding of the language so, having an idea on object-oriented languages such as C++ also helps. In short, if you know C or C++ it will be a little bit easier to cope with Java technology.

Read More

What is the difference between Object oriented programming and Object based programming?

Prabhas
Prabhas
Updated on 30-Jul-2019 21K+ Views

Many of us have a misconception that Java script is an object oriented language. But, the truth is Java Script is an Object Based Language. Object Based languages are different from Object Oriented Languages: Object Based Languages Object based languages supports the usage of object and encapsulation. They does not support inheritance or, polymorphism or, both. Object based languages does not supports built-in objects. Javascript, VB are the examples of object bases languages. Object Oriented Languages Object Oriented Languages supports all the features of Oops including inheritance and polymorphism. They support built-in objects. C#, Java, VB. Net ...

Read More
Showing 25371–25380 of 25,445 articles
Advertisements