Found 9326 Articles for Object Oriented Programming

What is Default access level in Java?

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

521 Views

The default access level is available when no access level is specified. All the classes, data members, methods etc. which have the default access level can only be accessed inside the same package.A program that demonstrates the default access level in Java is given as follows:Example Live Democlass Employee {    int empno;    String name;    void insert(int e, String n) {       empno = e;       name = n;    }    void display() {       System.out.println("Employee Number: " + empno);       System.out.println("Name: " + name);    } } public class ... Read More

Class that contains a String instance variable and methods to set and get its value in Java

Arushi
Updated on 30-Jul-2019 22:30:24

471 Views

A class declaration can contain a String instance and methods to set and get its value in Java.A program that demonstrates this is given as follows:Example Live Democlass Name {    private String name;    public void setName(String n) {       name = n;    }    public String getName() {       return name;    } } public class Demo {    public static void main(String[] args) {       Name n = new Name();       n.setName("John Smith");       System.out.println("The name is: " + n.getName());    } }OutputThe name is: John SmithNow ... Read More

Using run-time polymorphism in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

297 Views

A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.A program that demonstrates run-time polymorphism in Java is given as follows:Example Live Democlass Animal {    void sound() {       System.out.println("Animal makes sound");    } } class Cat extends Animal {    void sound() {       System.out.println("Cat Meows");    } } class Dog extends Animal {    void sound() {       System.out.println("Dog Barks");    } } class Cow extends Animal {    void ... Read More

Variable in subclass hides the variable in the super class in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

358 Views

Sometimes the variable in the subclass may hide the variable in the super class. In that case, the parent class variable can be referred to using the super keyword in Java.A program that demonstrates this is given as follows:Example Live Democlass A {    char ch; } class B extends A {    char ch;    B(char val1, char val2) {       super.ch = val1;       ch = val2;    }    void display() {       System.out.println("In Superclass, ch = " + super.ch);       System.out.println("In subclass, ch = " + ch);    } ... Read More

Demonstrate constructors in a Multilevel Hierarchy in Java

Arushi
Updated on 30-Jul-2019 22:30:24

1K+ Views

Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.A program that demonstrates constructors in a Multilevel Hierarchy in Java is given as follows:Example Live Democlass A {    A() {       System.out.println("This is constructor of class A");    } } class B extends A {    B() {       System.out.println("This is constructor of class B");    } } class C extends B {    C() {       System.out.println("This is constructor of class C"); ... Read More

Creating a Multilevel Inheritance Hierarchy in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

9K+ Views

Inheritance involves an object acquiring the properties and behaviour of another object. So basically, using inheritance can extend the functionality of the class by creating a new class that builds on the previous class by inheriting it.Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.A program that demonstrates a multilevel inheritance hierarchy in Java is given as follows:Example Live Democlass A {    void funcA() {       System.out.println("This is class A");    } } class B extends ... Read More

Role of super Keyword in Java Inheritance

Rishi Raj
Updated on 30-Jul-2019 22:30:24

447 Views

Parent class objects can be referred to using the super keyword in Java. It is usually used in the context of inheritance. A program that demonstrates the super keyword in Java is given as follows:Example Live Democlass A {    int a;    A(int x) {       a = x;    } } class B extends A {    int b;    B(int x, int y) {       super(x);       b = y;    }    void print() {       System.out.println("Value of a: " + a);       System.out.println("Value of b: " ... Read More

Deriving a Class in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

3K+ Views

A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance. It can also be said that it is used to extend the functionality of the class.A program that demonstrates this is given as follows:Example Live Democlass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A {    void funcB() {       System.out.println("This is class B");    } } public class Demo {   ... Read More

Create class Objects in Java using new operator

Arushi
Updated on 30-Jul-2019 22:30:24

470 Views

Class objects can be created in Java by using the new operator. A class is instantiated by the new operator by allocating memory for the object dynamically and then returning a reference for that memory. A variable is used to store the memory reference.A program that demonstrates this is given as follows:Example Live Democlass Student {    int rno;    String name;    public Student(int r, String n) {       rno = r;       name = n;    }    void display() {       System.out.println("Roll Number: " + rno);       System.out.println("Name: " + ... Read More

Add elements at the end of a Vector in Java

Arushi
Updated on 30-Jul-2019 22:30:24

308 Views

Elements can be added at the end of a Vector using the java.util.Vector.add() method. This method has a single parameter i.e. the element to be added. It returns true if the element is added to the Vector as required and false otherwise.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector(5);       for (int i = 1; i <= 10; i++) {          vec.add(i);       }       System.out.println("The Vector elements ... Read More

Advertisements