Found 9326 Articles for Object Oriented Programming

What are Java parent and child classes in Java?

usharani
Updated on 16-Jun-2020 08:47:34

14K+ Views

Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another.You can inherit the members of one class from another, use the extends keyword as:class A extends B{}The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).Following is an example which demonstrates inheritance. Here, we have two classes namely Sample and MyClass. Where Sample is the parent class and the class named MyClass is the child class.ExampleLive Democlass Sample{    public ... Read More

What are static members of a Java class?

varun
Updated on 30-Jul-2019 22:30:20

21K+ Views

In Java, static members are those which belongs to the class and you can access these members without instantiating the class. The static keyword can be used with methods, fields, classes (inner/nested), blocks. Static Methods − You can create a static method by using the keyword static. Static methods can access only static fields, methods. To access static methods there is no need to instantiate the class, you can do it just using the class name as − Example Live Demo public class MyClass { public static void sample(){ ... Read More

How to declare, define and call a method in Java?

Prabhas
Updated on 30-Jul-2019 22:30:20

2K+ Views

Following is the syntax to declare a method in Java. Syntax modifier return_type method_name(parameters_list){ //method body } Where, modifier − It defines the access type of the method and it is optional to use. return_type − Method may return a value. method_name − This is the method name. The method signature consists of the method name and the parameter list. parameters_list − The list of parameters, it is the type, order, and a number of parameters of a method. These are optional, method may contain zero parameters. method body − The method body defines ... Read More

What is the difference between simple name, canonical name and class name in a Java class?

vanithasree
Updated on 30-Jul-2019 22:30:20

2K+ Views

Canonical name of a Java class is the name of the class along with the package. For example, the canonical name of the class File is java.io.File. You can also get the canonical name of a particular class using Java method. The class named Class provides a method getCanonicalName(), this method returns canonical name of the current class. Example Live Demo import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); ... Read More

How to access the members of a class from another class in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:20

11K+ Views

To access the members of a class from other class. First of all, import the class. Create an object of that class. Using this object access, the members of that class. Suppose there is a class in a package called myPackage with a method named display() package myPackage; public class Sample { public void display() { System.out.println("Hello"); } } You can access it as. import myPackage; public class MyClass { public static void main(String args[]) { Sample s = new Sample(); s.done(); } } Output hello

Why do Java array declarations use curly brackets?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

480 Views

Curly brackets usually denote sets and ensembles while parenthesis usually in most Algol-based programming languages curly braces are used to declare arrays.

How to declare a class in Java?

mkotla
Updated on 30-Jul-2019 22:30:20

12K+ Views

Following is the syntax to declare a class. class className { //Body of the class } You can declare a class by writing the name of the next to the class keyword, followed by the flower braces. Within these, you need to define the body (contents) of the class i.e. fields and methods.To make the class accessible to all (classes) you need to make it public. public class MyClass { //contents of the class (fields and methods) }

How to convert a byte array to a hex string in Java?

Abhinaya
Updated on 30-Jul-2019 22:30:20

380 Views

The printHexBinary() method of the DatatypeConverter class accepts a byte array and returns a hex string.Exampleimport javax.xml.bind.DatatypeConverter; public class ByteToHexString { public static void main(String args[]) { String sam = "Hello how are you how do you do"; byte[] byteArray = sam.getBytes(); String hex = DatatypeConverter.printHexBinary(byteArray); System.out.println(hex); } }Output48656C6C6F20686F772061726520796F7520686F7720646F20796F7520646F

What are Java classes?

Giri Raju
Updated on 30-Jul-2019 22:30:20

531 Views

A class in Java is a user-defined datatype, a blueprint, a classification, that describes the behavior/state that the object of its type support. Example public class Dog { String breed; int age; String color; void barking() { } void hungry() { } void sleeping() { } } A class can contain any of the following variable types. Local variables − Variables defined inside methods, constructors or blocks are called local ... Read More

How to convert BLOB to Byte Array in java?

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

6K+ Views

You can contents of a blob into a byte array using the getBytes() method.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Arrays; public class BlobToByteArray { public static void main(String[] args) throws Exception { Image image = new BufferedImage(300, 400, BufferedImage.TYPE_INT_RGB); String JDBC_DRIVER = "com.mysql.jdbc.Driver"; String DB_URL = "jdbc:mysql://localhost/mydb"; String USER = "root"; String PASS = "password"; ... Read More

Advertisements