Found 2616 Articles for Java

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

595 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.

How to load classes at runtime from a folder or Java package

Abhinaya
Updated on 04-Feb-2020 11:06:03

669 Views

Using CLASSPATH, you can load any classes at runtime.Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as −\sources\com\apple\computers\Dell.java \classes\com\apple\computers\Dell.classBy doing this, it is possible to give access to the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the ... Read More

How to use sub-package in Java?

Sravani S
Updated on 04-Feb-2020 11:12:22

880 Views

Subpackages are similar to sub-directories. Consider an example. The company had a com.apple.computers package that contained a Dell.java source file, it would be contained in a series of subdirectories like this −....\com\apple\computers\Dell.javaAt the time of compilation, the compiler creates a different output file for each class, interface, and enumeration defined in it. The base name of the output file is the name of the type, and its extension is .class.For example −// File Name:Dell.java package com.apple.computers; public class Dell { } class Ups { }Now, compile this file as follows using -d option −$javac -d.Dell.javaThe files will be compiled as ... Read More

Advertisements