Abstract Classes and Methods

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Example of class that implements several interfaces

HashMap HashMap class implements several interfaces and also extends the abstract class AbstractMap.

difference between interface&abstract class

abstract class : - can declare fields that are not static and final -can define public, protected, and private concrete methods. -can extend only one class whether or not it is abstract interface: - all fields are automatically public, static, and final - all methods that you declare or define (as default methods) are public. -implement any number of interfaces

When does an abstract Class implement an interface

abstract class X implements Y { // implements all but one method of Y } class XX extends X { // implements the remaining method in Y } In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.

Difference between array and arraylist

array: of fixed size arraylist: may increase or decrease on the basis of size.

String.valueOf

(String.valueOf(productId)) returns string representation of the int argument

When to use an abstract class?

- want to share code among several closely related classes -You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private). -You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

When to use interface?

-You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. You want to take advantage of multiple inheritance of type.

Implementation of methods

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

What is the difference between pass by value and pass by reference?

When passing an argument to a method, Java will create a copy of the value inside the original variable and pass that to the method as arguments - and that is why it is called pass by value

Does java pass by reference or pass by value?

Pass by value

Example of interface in JDK

Serialization, Cloneable, Map<K,V>. These are implemented by HashMap

Methods in comparator interface

" compare" and " equals". The first method compares its two input arguments and imposes an order between them. It returns a negative integer, zero, or a positive integer to indicate that the first argument is less than, equal to, or greater than the second. The second method requires an Object as a parameter and aims to decide whether the input object is equal to the comparator. The method returns true, only if the specified object is also a comparator and it imposes the same ordering as the comparator.

Declare Arraylist Like instance variable ArrayList class supports only object types and not primitive types.

// declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; Arraylist<Integer> productId; productId = new Arraylist<Integer>

Methods in interface

A class that implements an interface must implement all the methods declared in the interface

AbstractMap

AbastractMap defines methods like get, put, isEmpty, containsKey, and containsValue that is shared by its subclasses.

Reference types

Employee e1 = new Employee("Ram", "Niraula",34); e1 is the object that has memory address of ("Ram", "Niraula",34) e1=4032(lets say its the memory address)

Arraylist extends from which Abstract class Implemented from which interface

AbstractList List

Example of abstract class in JDK

AbstractMap(it is part of Collections Framework)

Abstract class

An abstract class is a class that is declared abstract—it may or may not include abstract methods.

Abstract method

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY);

Arraylist supports three constructors

Arraylist() -builds an empty arraylist Arraylist(Collection c)-initialized with the elements of collection c Arraylist(int capacity) Has fixed value. The capacity grows automatically as elements are added to an array list.

Similarity between interface&abstract class

Cannot instantiate them contains mix of methods declared with or without implementation

what is a type?

Every variable in Java has a type. Type tells Java how this variable should be treated How much memory should be allocated for that variable Every object in java has a type.(Employee e1) Empoyee is the type for the object e1 e1 is also called class type int a a is called the class type Class type, Object type, Reference type - all means the object

Abstract Map contains which subclasses

HashMap, TreeMap & ConcurrentHashMap

Super

If your method overrides one of its superclass's method, you can invoke the overridden method using the keyword super. "super. method" will print the info from superclass

Rules for Overriding

In java, a method can only be written in Subclass, not in same class. The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class

difference between inheritance & polymorphism

Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the base class or super class) with another class (called the derived class or subclass). Polymorphism is a run-time mechanism, that the existence of an object in more than one forms. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.Run time Decision is made based on objects.... To achieve Polymorphism inheritance is must , But to achieve inheritance polymorphism is not must.

what is multiple inheritance

Multiple inheritance is the ability of a single class to inherit from multiple classes. Java does not have this capability.

Method in comparable interface

compareTo Syntax: public int compareTo(Object obj) This method compares two objects, in order to impose an order between them. Specifically, it returns a negative integer, zero, or a positive integer to indicate that the input object is less than, equal or greater than the existing object.

difference between overriding & overloading

http://www.programmerinterview.com/index.php/java-questions/method-overriding-vs-overloading/ overriding- run time Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class). Advantages of Overriding in Java: 1. It is used for run time polymorphism. 2. It is used to provide specific implementation of a method that is already provided by its super class. overloading - compile time Overloading in Java is the ability to create multiple methods of the same name, but with different parameters. The main advantage of this is cleanliness of code. static String valueOf(boolean b) static String valueOf(char c) This means that if we have any type of variable, we can get a String representation of it by using String.valueOf(variable)

Arraylist Full Info

http://www.tutorialspoint.com/java/java_arraylist_class.htm

Initialize an array

int data[] = new int[] {10,20,30,40,50,60,71,80,90,91 }; or int data[]; data=new int[] {10,20,30,40,50,60,71,80,90,91 };

Why we can't instantiate a abstract class in Java?

n Abstract class represents an abstract concept. Take your vehicle example. You cannot build a vehicle that is not something more specific. You can have a set of vehicles, that could be made of 2004 corolla's and '98 ford escorts and 1984 cs36 (a kind of yacht), a mark 4 firefly class mid-range bulk transport(the one with the stabilizers), you can take any one of those individually and call them a vehicle but you cannot have something that is only a vehicle and not one of those or some other specific type of vehicle. Abstract classes represent such abstract concepts as vehicle. Hence the idea of instantiating one is non-sensical because to actually instantiate it you need to know what you're instantiating. You can't instantiate abstract class because it's just to give a structure your class which is extending it.

replaceAll

productName.replaceAll("\\s","") //removes all the whitespaces of productName String str = "This 1231 is 124 a String 1243 to 34563 use 5455"; //remove all numbers String newStr = str.replaceAll("[0-9]+", ""); System.out.println(newStr); //remove all words newStr = str.replaceAll("[a-zA-Z]+", "Java"); System.out.println(newStr); Output This is a String to use Java 1231 Java 124 Java Java 1243 Java 34563 Java 5455

Substring Syntax

public String substring(int beginIndex, int endIndex) count all spaces or public String substring(int beginIndex) http://www.tutorialspoint.com/java/java_string_substring.htm

Comparable syntax

public class Order implements Comparable<Order> Order is the class

what is dynamic binding

the process in which the method implementation that is actually called is determined at run-time not compile-time http://www.programmerinterview.com/index.php/java-questions/dynamic-binding/


संबंधित स्टडी सेट्स

NURS 3110: Unit VIII Medications

View Set

ATI: Normal Physiological Changes During Pregnancy

View Set

introduction to the Science of Psychology

View Set

Патофизиология 2020 экзамен тесты

View Set