Java Articles

Page 316 of 450

inheritance(is-a) v/s composition (has-a) relationship in Java

Giri Raju
Giri Raju
Updated on 17-Jun-2020 979 Views

IS-A RelationshipIS-A is a way of saying − This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }Now, if we consider the IS-A relationship, we can say −Mammal IS-A AnimalReptile IS-A AnimalDog IS-A MammalHence: Dog IS-A Animal as wellWith the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the ...

Read More

inheritance(is-a) v/s composition (has-a) relationship in Java

Giri Raju
Giri Raju
Updated on 17-Jun-2020 979 Views

IS-A RelationshipIS-A is a way of saying − This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }Now, if we consider the IS-A relationship, we can say −Mammal IS-A AnimalReptile IS-A AnimalDog IS-A MammalHence: Dog IS-A Animal as wellWith the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the ...

Read More

inheritance(is-a) v/s composition (has-a) relationship in Java

Giri Raju
Giri Raju
Updated on 17-Jun-2020 979 Views

IS-A RelationshipIS-A is a way of saying − This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }Now, if we consider the IS-A relationship, we can say −Mammal IS-A AnimalReptile IS-A AnimalDog IS-A MammalHence: Dog IS-A Animal as wellWith the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the ...

Read More

Exception handling with method overriding in Java.

usharani
usharani
Updated on 17-Jun-2020 4K+ Views

Yes, we can override a method by changing only the exception handling in java pertaining the following rule −An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

Read More

Exception handling with method overriding in Java.

usharani
usharani
Updated on 17-Jun-2020 4K+ Views

Yes, we can override a method by changing only the exception handling in java pertaining the following rule −An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

Read More

Exception handling with method overriding in Java.

usharani
usharani
Updated on 17-Jun-2020 4K+ Views

Yes, we can override a method by changing only the exception handling in java pertaining the following rule −An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.

Read More

What are all the ways an object can be created in Java?

varun
varun
Updated on 16-Jun-2020 256 Views

You can create an objectUsing new keyword.Sample obj = new Sample();Using the newInstance() method and Class.forName() method.Sample obj2 = (Sample) Class.forName("Sample").newInstance();Using the clone() method by implementing Cloneable Interface (marker).Sample obj3 = (Sample) obj1.clone();Using class loader.Object obj4 = Sample.class.getClassLoader().loadClass("Sample");Using the constructor class from lang.reflect.Class cls = Sample.class; Constructor obj = cls.getDeclaredConstructors()[0]; Sample obj5 = (Sample) obj.newInstance();

Read More

How to count Java comments of a program that is stored in a text file?

Samual Sam
Samual Sam
Updated on 16-Jun-2020 592 Views

You can read the contents of a file using the Scanner class and You can find the comments in a particular line using contains() method.Exampleimport java.io.*; import java.util.Scanner; public class FindingComments {    public static void main(String[] args) throws IOException {       Scanner sc = new Scanner(new File("HelloWorld"));       String input;       int single = 0;       int multiLine = 0;       while (sc.hasNextLine()) {          input = sc.nextLine();          if (input.contains("/*")) {             multiLine ++;       ...

Read More

What are variadic functions in Java?

Monica Mona
Monica Mona
Updated on 16-Jun-2020 1K+ Views

Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions. Example public class Sample {     void demoMethod(String... args) {         for (String arg: args) {             System.out.println(arg);         }     }     public static void main(String args[] ){         new Sample().demoMethod("ram", "rahim", "robert");         new Sample().demoMethod("krishna", "kasyap");     } } Output ram rahim robert krishna kasyap

Read More

Java labelled for loop

Vikyath Ram
Vikyath Ram
Updated on 15-Jun-2020 2K+ Views

Following program is using labeled for loops. Example public class Tester {     public static void main(String args[]) {                first:            for (int i = 0; i 

Read More
Showing 3151–3160 of 4,498 articles
« Prev 1 314 315 316 317 318 450 Next »
Advertisements