Found 338 Articles for Java Programming

Covariant return types in Java

varma
Updated on 24-Feb-2020 09:02:18

8K+ Views

Covariant return type refers to return type of an overriding method. It allows to narrow down return type of an overridden method without any need to cast the type or check the return type. Covariant return type works only for non-primitive return types.From Java 5 onwards, we can override a method by changing its return type only by abiding the condition that return type is a subclass of that of overridden method return type.Following example showcases the same.Example Live Democlass SuperClass {    SuperClass get() {       System.out.println("SuperClass");       return this;    } } public class Tester ... Read More

Exception handling with method overriding in Java.

usharani
Updated on 17-Jun-2020 07:10:18

3K+ 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.

method overloading and type promotion in Java

radhakrishna
Updated on 17-Jun-2020 06:54:02

1K+ Views

Method overloading helps to create multiple methods with the same name to do similar action on a different type of parameters.We can use type promotion in case variables are of similar type. Type promotion automatically promotes the lower range value to higher range value. For example, byte variable can be assigned to an int variable. Here byte variable will be type promoted to int. In case, we want to add two numbers which can be byte, short or int, we can use a single method. See the example below −Example Live Demopublic class Tester {    public static void main(String args[]) ... Read More

Different ways to overload a method in Java

mkotla
Updated on 17-Jun-2020 06:53:10

329 Views

Method overloading can be achieved in following three ways −By changing the number of parameters in the method.By changing the order of parameter typesBy changing the data types of the parameters.See the example below−Example Live Demopublic class Tester {    public static void main(String args[]) {       Tester tester = new Tester();       System.out.println(tester.add(1, 2));       System.out.println(tester.add(1, 2, 3));       System.out.println(tester.add(1.0f, 2, 3));       System.out.println(tester.add(1, 2.0f, 3));    }    public int add(int a, int b) {       return a + b;    }    public int add(int a, ... Read More

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

Giri Raju
Updated on 17-Jun-2020 07:11:07

598 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

HAS-A relationship in Java

Abhinanda Shri
Updated on 04-Feb-2020 12:06:28

1K+ Views

These relationships are mainly based on the usage. This determines whether a certain class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.Let's look into an example −Examplepublic class Vehicle{} public class Speed{} public class Van extends Vehicle {    private Speed sp; }This shows that class Van HAS-A Speed. By having a separate class for Speed, we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple applications.In an Object-Oriented feature, the users do not need ... Read More

Why multiple inheritance is not supported in Java

Abhinaya
Updated on 07-Sep-2023 00:44:29

38K+ Views

In Java, a class cannot extend more than one class. Therefore following is illegal −Examplepublic class extends Animal, Mammal{}However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritances.The reason behind this is to prevent ambiguity.Consider a case where class B extends class A and Class C and both class A and C have the same method display().Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.

Multilevel inheritance in Java

Govinda Sai
Updated on 31-Oct-2023 14:34:48

60K+ Views

Multilevel inheritance - A class inherits properties from a class which again has inherits properties.Example Live Democlass Shape {    public void display() {       System.out.println("Inside display");    } } class Rectangle extends Shape {    public void area() {       System.out.println("Inside area");    } } class Cube extends Rectangle {    public void volume() {       System.out.println("Inside volume");    } } public class Tester {    public static void main(String[] arguments) {       Cube cube = new Cube();       cube.display();       cube.area();       cube.volume();    } }OutputInside display Inside area Inside volume

Single level inheritance in Java

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

11K+ Views

Single Level inheritance - A class inherits properties from a single class. For example, Class B inherits Class A.Example Live Democlass Shape {    public void display() {       System.out.println("Inside display");    } } class Rectangle extends Shape {    public void area() {       System.out.println("Inside area");    } } public class Tester {    public static void main(String[] arguments) {       Rectangle rect = new Rectangle();       rect.display();       rect.area();    } }OutputInside display Inside areaHere Rectangle class inherits Shape class and can execute two methods, display() and area() as shown.

How to put two public classes in a Java package.

Srinivas Gorla
Updated on 30-Jul-2019 22:30:21

333 Views

Yes. The only condition is to have one public class in separate java file.

Advertisements